Python | Ways to Convert a List of ASCII Values ​​to a String



Method # 1: Using the naive method

# Python code for demo
# convert ascii list of values ​​
# string

 
# Initializing list

ini_list = [ 71 , 101 , 101 , 107 , 115 , 102

111 , 114 , 71 , 101 , 101 , 107 , 115

 
# Print initial list

print ( "Initial list" , ini_list)

 
# Using the naive method

res = " "

for val in ini_list:

  res = res + chr (val)

  
# Print the resulting string

print ( "Resultant string" , str (res))

Exit :

Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115 ]
Resultant string Python.Engineering

Method # 2: Using the map ()

# Python code for demonstration
# pr Ascii value list conversion
# string

 
# Initializing list

ini_list = [ 71 , 101 , 101 , 107 , 115 , 102 ,

111 , 114 , 71 , 101 , 101 , 107 , 115

 
# Print start list

print ( "Initial list" , ini_list)

 
# Use the map and join

res = `` .join ( map ( chr , ini_list))

 
# Print the resulting string

print ( "Resultant string" , str (res))

Exit :

Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115]
Resultant string Python.Engineering

Method # 3: Using list join and comprehension

# Python code for demonstration
# ascii list of values ​​conversion
# string

 
# Initializing list

ini_list = [ 71 , 101 , 101 , 107 , 115 , 102 ,

111 , 114 , 71 , 101 , 101 , 107 , 115

 
# Print starting list

print ( "Initial list" , ini_list)

 
# And use comprehension list and join

res = `` .join ( chr (val) for val in ini_list)

 
# Print the resulting string

print ( "Resultant string" , str (res))

Exit :

Initial list [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107 , 115]
Resultant string Python.Engineering