Convert a List to a String in Python

In this tutorial, we will learn how to convert a list to a string in Python.

Table Of Contents

As we know a list is a data structure in Python that stores the elements of multiple data types. In python, we can create a list using list() or [].
Now we will see how to convert the given list to a string.

Convert list to string using for-loop

Here, we are using a for loop to iterate over a python list. During iteration, we will concatenate all the elements present in the list to string using the assignment operator. In this way, we are converting the entire list to a string.

Syntax:

for ele in list1:
    string1=string1+ele

Here,

  1. list1 is the input list
  2. ele is the iterator, used to iterate the list using for loop.
  3. string1 is the empty string variable and it will store each string present in the list through iterator ele.

Example:

In this example, we are creating a list that has 3 strings and we will convert the list into a string.

# Consider a list with 3 strings
list1 = ['welcome','to','thisPointer']

print("Actual List",list1)

# Create an empty string 
string1 = ''

# Use for loop to iterate the list1.
for ele in list1:
  string1 = string1 + ele

#display the string.
print(string1)

Output:

Actual List ['welcome', 'to', 'thisPointer']
welcometothisPointer

In the above example, three strings – ‘welcome’, ‘to’, ‘thisPointer’ are converted to a string.

Convert list to string using join() method

The join() in Python can be used joins the strings in a list. We can use an empty string as delimeter by calling the join() method on it. In the argument we can pass a list of strings. The join() method will join all the string items in that list and returns the joined string.

Syntax:

string1.join(list1)

Here,

  1. list1 is the input list
  2. string1 is the empty string variable. It joins the strings present in the list one by one.

Example:

In this example, we are creating a list that has 3 strings and we will convert the list into a string.

# Consider a list with 3 strings
list1 = ['welcome','to','thisPointer']

print("Actual List: ",list1)

# Create an empty string 
string1 = " "

# Join the strings present in the list and display
mergedStr = string1.join(list1)

print("String: ", mergedStr)

Output:

Actual List:  ['welcome', 'to', 'thisPointer']
String:  welcome to thisPointer

In the above example, three strings – ‘welcome’, ‘to’, ‘thisPointer’ are converted into a string.

Convert list to string using list comprehension

We will iterating over all the strings in a list using a list comprehension and joining these items into a string using join() method. So it is important to place join() method infront of the list comprehension.

Syntax:

' '.join([ele for ele in list1])

Here,

  1. list1 is the input list
  2. ele is the iterator used to iterate the strings in the list1.

Example:

In this example, we are creating a list that has 3 strings and we will convert the list into a string.

# Consider a list with 3 strings
list1=['welcome','to','thisPointer']

print("Actual List: ",list1)

# Join the strings present in the list and display
mergedStr = ' '.join([ele for ele in list1])

print("String: ", mergedStr)

Output:

Actual List:  ['welcome', 'to', 'thisPointer']
String:  welcome to thisPointer

In the above example, three strings – ‘welcome’, ‘to’, ‘thisPointer’ are converted into a string. This method is usefull when you want to do some filtering while selecting strings from the list for joining.

Convert lits to string Using map()

The map() is used to map the list of strings to a string using the join() function.

Syntax:

' '.join(map(str, list1))

Here,

  1. list1 is the input list
  2. str is the first parameter that represents the string function.

Example:

In this example, we are creating a list that has 3 strings and we will convert the list into a string. It is also poosible to convert a list of non-string type items to a string using this apprach. Here map() function will convert each item of list to string first using the str() function and then join function will join all those converted strings to a single string.

# Consider a list with 3 strings
list1 = ['welcome','to','thisPointer']

print("Actual List: ",list1)

# Join the strings present in the list and display
mergedStr = ' '.join(map(str, list1))

print("String: ", mergedStr)

Output:

Actual List:  ['welcome', 'to', 'thisPointer']
String:  welcome to thisPointer

In the above example, three strings – ‘welcome’, ‘to’, ‘thisPointer’ are converted to a string.

Summary

In this article, we learned to convert a list to a string using list comprehension, map(), join, and for loop. Based on the utility, you can use a particular method to convert a list to a string in Python. Thanks.

Leave a Comment

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

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

Scroll to Top