Skip to content
Related Articles

Related Articles

Python | Ways to convert list of ASCII value to string

View Discussion
Improve Article
Save Article
  • Last Updated : 29 Jun, 2019
View Discussion
Improve Article
Save Article

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!