In this Python tutorial, we will learn how to convert a list of characters into a string,
Table Of Contents
Convert a list of characters into a string using for loop
Here, we will convert all the characters from a list into a string using for loop. So we will iterate all the characters in the list using an iterator inside for loop. During iteration, we will add each character to an new empty string.
Example 1:
# Consider the list of characters list1=['W','e','l','c','o','m','e','','t','o','','p','y','t','h','o','n'] finalStr="" # Actual List print("Original List of characters: ", list1) # Convert the character list to string for character in list1: finalStr = finalStr + character print("String: ", finalStr)
Output:
Original List of characters: ['W', 'e', 'l', 'c', 'o', 'm', 'e', '', 't', 'o', '', 'p', 'y', 't', 'h', 'o', 'n'] String: Welcometopython
We can see that all characters are converted into a string.
Example 2:
Frequently Asked:
# Consider the list of characters list1=['1','2','3'] finalStr = "" # Actual List print("Original List of characters: ", list1) # Convert the character list to string for character in list1: finalStr=finalStr + character print("String: ", finalStr)
Output:
Original List of characters: ['1', '2', '3'] String: 123
We can see that all characters are converted into a string.
Convert a list of characters into a string using join()
Here, we will join all the characters into a string using the join() method. We will add the empty string as a delimeter while joining all characters from list.
syntax:
finalStr = delimeter.join(list1)
Here, the delimeter is the empty string that will be used as delimeter while joining the characters in list. It returns a new joined string.
Example 1:
# Consider the list of characters list1=['W','e','l','c','o','m','e','','t','o','','p','y','t','h','o','n'] # Actual List print("Original List of characters: ", list1) # Convert the character list to string finalStr = "".join(list1) print("String: ", finalStr)
Output:
Original List of characters: ['W', 'e', 'l', 'c', 'o', 'm', 'e', '', 't', 'o', '', 'p', 'y', 't', 'h', 'o', 'n'] String: Welcometopython
We can see that all characters are converted into a string.
Example 2:
# Consider the list of characters list1=['1','2','3'] # Actual List print("Original List of characters: ", list1) # Convert the character list to string finalStr = "".join(list1) print("String: ", finalStr)
Output:
Original List of characters: ['1', '2', '3'] String: 123
We can see that all characters are converted into a string.
Convert a list of characters into a string using reduce()
Here, we will concatenate all the characters from a list into a string using the reduce() method. It takes a lambda expression and a list as a parameters. Then aggregates all the values in list/sequence using the given lambda function. We can pass a lambda function that accepts two string arguments and returns a joined string.
It is available in the functools module. So it is necessary to import the functions module.
syntax:
functools.reduce(lambda i,j : i+j, list1)
Here, reduce() method will take the lambda expression and a list as a parameters to concatenate the characters present in the list1.
Example 1:
import functools # Consider the list of characters list1=['W','e','l','c','o','m','e','','t','o','','p','y','t','h','o','n'] # Actual List print("Original List of characters: ",list1) # Convert the character list to string finalStr = functools.reduce(lambda i,j : i+j, list1) print("String: ", finalStr)
Output:
Original List of characters: ['W', 'e', 'l', 'c', 'o', 'm', 'e', '', 't', 'o', '', 'p', 'y', 't', 'h', 'o', 'n'] String: Welcometopython
We can see that all charcaters are converted into a string.
Example 2:
import functools # Consider the list of characters list1=['1','2','3'] # Actual List print("Original List of characters: ",list1) # Convert the character list to string finalStr = functools.reduce(lambda i,j : i+j, list1) print("String: ", finalStr)
Output:
Original List of characters: ['1', '2', '3'] String: 123
We can see that all characters are converted into a string.
Summary
In this article, we saw how to convert a list of characters into a string. We have seen three methods to convert a list of characters into a string. First is using for loop. In that we iterated over all the characters in a list using an iterator and added them to the string. Then in second solution, using the join() method, we added every character from list to the an empty string. In the last method, using reduce() with the lambda expression, we concatenated all the characters in list to a string. Thanks.