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 list ini_list = [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] # Printing initial list print ("Initial list", ini_list) # Using Naive Method res = "" for val in ini_list: res = res + chr(val) # Printing resultant string print ("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 list ini_list = [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] # Printing initial list print ("Initial list", ini_list) # Using map and join res = ''.join(map(chr, ini_list)) # Print the resultant string print ("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 list ini_list = [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] # Printing initial list print ("Initial list", ini_list) # Using list comprehension and join res = ''.join(chr(val) for val in ini_list) # Print the resultant string print ("Resultant string", str(res)) |
Output:
Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115]
Resultant string GeeksforGeeks
Recommended Posts:
- Python | Ways to Convert a 3D list into a 2D list
- Python | Ways to convert string to json object
- Python | Convert list of numerical string to list of Integers
- Python | Convert list of string into sorted list of integer
- Python | Convert a string representation of list into list
- Python | Convert string enclosed list to list
- Python | Convert list of string to list of list
- Python | Program to convert String to a List
- Python program to convert a list to string
- Python | Convert a list of characters into a string
- Python | Convert list of strings to space separated string
- Python | Ways to convert hex into binary
- Python | Check for ASCII string
- Python | Ways to split a string in different ways
- Python | Ways to convert Boolean values to integer
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



