Convert a List of Characters into a String in Python

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:

Advertisements
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']

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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top