Python | Ways to convert list of ASCII value to string
Given a list of ASCII values, write a Python program to convert those values to their character and make a string. Given below are a few methods to solve the problem.
Method #1: Using Naive Method
# Python code to demonstrate # conversion of list of ascii values# to string # Initialising listini_list = [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] # Printing initial listprint ("Initial list", ini_list) # Using Naive Methodres = ""for val in ini_list: res = res + chr(val) # Printing resultant stringprint ("Resultant string", str(res)) |
Output:
Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115]
Resultant string GeeksforGeeks
Method #2: Using map()
# Python code to demonstrate # conversion of list of ascii values# to string # Initialising listini_list = [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] # Printing initial listprint ("Initial list", ini_list) # Using map and joinres = ''.join(map(chr, ini_list)) # Print the resultant stringprint ("Resultant string", str(res)) |
Output:
Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115]
Resultant string GeeksforGeeks
Method #3: Using join and list comprehension
# Python code to demonstrate # conversion of a list of ascii values# to string # Initialising listini_list = [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] # Printing initial listprint ("Initial list", ini_list) # Using list comprehension and joinres = ''.join(chr(val) for val in ini_list) # Print the resultant stringprint ("Resultant string", str(res)) |
Output:
Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115]
Resultant string GeeksforGeeks



.png)