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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:



Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115]
Resultant string GeeksforGeeks

Method #2: Using map()

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.