Convert a list of characters into a string in Python

In this tutorial, we are going to learn how to convert a list of characters into a string in Python. There are many ways to do this in Python. We will have a look at a few examples to demonstrate some of these methods in this post.

Method 1

In this method, we will first initialize an empty string and then traverse through the list and add all the characters in the empty string. Once all the characters have been added, we can print it. See the below code to understand it fully.

list_of_char = ['c', 'o', 'd', 'e', 's', 'p', 'e', 'e', 'd', 'y']

str = ""

for char in list_of_char:
    str = str + char

print(str)

Output:

codespeedy

Method 2

This method includes the use of the join() method. Have a good look at the given example code and the output.

list_of_char = ['c', 'o', 'd', 'e', 's', 'p', 'e', 'e', 'd', 'y']

str = ""

print(str.join(list_of_char))

Output:

codespeedy

Note that str = “” ensures that there are no spaces between the characters when we join them.

Method 3

Another method to convert a list of characters into a string has been given here. In this method, we will use concat() and reduce() functions to accomplish this. See the code.

Read about the reduce() function here: Python reduce() function

from functools import reduce
from operator import concat

list_of_char = ['c', 'o', 'd', 'e', 's', 'p', 'e', 'e', 'd', 'y']

print(reduce(concat, list_of_char))

Output:

codespeedy

Thank you and keep coding.

Also read: How to concatenate two strings in Python

Leave a Reply

Your email address will not be published. Required fields are marked *