Ways to Convert List of ASCII Value to String - Python
Last Updated : 03 Feb, 2025
The task of converting a list of ASCII values to a string in Python involves transforming each integer in the list, which represents an ASCII code, into its corresponding character. For example, with the list a = [71, 101, 101, 107, 115], the goal is to convert each value into a character, resulting in the string "Geeks".
Using map()
map() can be used with the chr() function to convert each ASCII value to its corresponding character. This method is efficient because map() applies the function directly to each element in the list, and when combined with ''.join(), it avoids the overhead of repeated string concatenation, making it ideal for this task.
Python a = [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] res = ''.join(map(chr, a)) print(res)
Explanation: map(chr, a) converts each ASCII value in the list a to its corresponding character and ''.join() then combines all the characters into a single string without any separators.
Using list comprehension
For converting ASCII values to characters, a list comprehension can be used in conjunction with chr() to generate a list of characters, which is then joined into a single string using ''.join(). This approach is both efficient and Pythonic, often preferred for its readability and clarity.
Python a = [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] res = ''.join([chr(val) for val in a]) print(res)
Explanation: [chr(val) for val in a] converts each ASCII value in a to a character and ''.join() combines them into a single string.
Using bytearray
We can create a bytearray from the list of ASCII values and then decode it to obtain the corresponding string. This method is optimal for handling byte-oriented tasks and avoids the overhead of string concatenation.
Python a = [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] res = bytearray(a).decode() print(res)
Explanation: bytearray(a) creates a bytearray from the list a and .decode() converts the bytearray into a string by interpreting the bytes as characters.
Using for loop
A traditional approach to convert ASCII values to a string is to use a for-loop to iterate through the list, apply chr() to each ASCII value and concatenate the results into a string. While straightforward, this method is less efficient because it repeatedly creates new string objects during each concatenation .
Python a = [71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] res = "" # initialize empty string for val in a: res += chr(val) print(res)
Explanation: for loop iterates through each value in the list a, converts each ASCII value to a character using chr(val) and appends it to the res string using +=.
Similar Reads
Convert String List to ASCII Values - Python We need to convert each character into its corresponding ASCII value. For example, consider the list ["Hi", "Bye"]. We want to convert it into [[72, 105], [66, 121, 101]], where each character is replaced by its ASCII value. Let's discuss multiple ways to achieve this.Using List Comprehension with o
2 min read
Python Program to Convert a List to String In Python, converting a list to a string is a common operation. In this article, we will explore the several methods to convert a list into a string. The most common method to convert a list of strings into a single string is by using join() method. Let's take an example about how to do it.Using the
3 min read
Convert List of Tuples to List of Strings - Python The task is to convert a list of tuples where each tuple contains individual characters, into a list of strings by concatenating the characters in each tuple. This involves taking each tuple, joining its elements into a single string, and creating a new list containing these strings.For example, giv
3 min read
Convert list of strings to list of tuples in Python Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Con
5 min read
Python | List of tuples to String Many times we can have a problem in which we need to perform interconversion between strings and in those cases, we can have a problem in which we need to convert a tuple list to raw, comma separated string. Let's discuss certain ways in which this task can be performed. Method #1: Using str() + str
8 min read
Python - List of float to string conversion When working with lists of floats in Python, we may often need to convert the elements of the list from float to string format. For example, if we have a list of floating-point numbers like [1.23, 4.56, 7.89], converting them to strings allows us to perform string-specific operations or output them
3 min read