Python – List To String Conversion

This is a detailed tutorial to convert a list into a string in Python. This article explains four different methods to join a list and form its string.

There are many use cases and applications when you need to convert the elements of a list together to form their string. In Python, you can do this in different ways. four different methods for the list-to-string conversion are illustrated below with the help of examples.

So, for a demonstration, I’m taking two lists with dummy data and we’ll convert these two lists into strings using four different methods.

words = ["Hello", "my", "name", "is", "Gurmeet" ,"Singh"]
line = ["The", "distance", "between", "Ludhiana", "and", "Chandigarh", "is", 90, "kms."]

The variables words and line holds two Python Lists.

Method 1. Combine Elements Using Iteration

You can use any loop to iterate over the python list and by doing Python For Loop.

words = ["Hello", "my", "name", "is", "Gurmeet" ,"Singh"]
line = ["The", "distance", "between", "Ludhiana", "and", "Chandigarh", "is", 90, "kms."]

wordsString = ""
lineString = ""

for word in words:
    wordsString += word
    #To Add Space After Each Word
    wordsString += " "

for one in line:
    #str function converts any numerical value to string
    lineString += str(one)
    #To Add Space After Each Word
    lineString += " "

print(wordsString)
print(lineString)

List To String Conversion Using Iteration Method

Note. As the second list contains an Integer value, so to avoid the error of data type mismatch during concatenation, I’ve used str() function to convert every list item to a string.

Method 2. Using .join() Method

Every string type variable can make use of the object type method .join() to convert an iterable like list into a string. You’ve to first create a string and have to apply the join method by providing an iterable formation as a parameter to this method. The string you first created will be used to separate the list elements in the resultant string.

The following example illustrates the use of the join method for the list-to-string conversion.

words = ["Hello", "my", "name", "is", "Gurmeet" ,"Singh"]
line = ["The", "distance", "between", "Ludhiana", "and", "Chandigarh", "is", 90, "kms."]

space = " "

wordsString = space.join(words)
lineString = space.join(str(word) for word in line)

print(wordsString)
print(lineString)

Python List To String Using JOIN Method

Method 3. Using List Comprehension

Note. This is already illustrated in the above example for the second list.

line = ["The", "distance", "between", "Ludhiana", "and", "Chandigarh", "is", 90, "kms."]
space = " "
lineString = space.join(str(word) for word in line)
print(lineString)

If all the elements of your list belong to the string data type, then you can directly provide the list as an argument to this function, otherwise, you must use the List Comprehension technique to first convert the list into all string type elements list and then have to provide to this function.

Method 4. Using map() Function

You can also apply map() function along with with the .join() method. The following example illustrates the use of the method map() to convert a list into a string.

words = ["Hello", "my", "name", "is", "Gurmeet" ,"Singh"]
line = ["The", "distance", "between", "Ludhiana", "and", "Chandigarh", "is", 90, "kms."]

space = " "

wordsString = space.join(map(str, words)) 
lineString = space.join(map(str,line))

print(wordsString)
print(lineString)

Converting Python List To String Using Join & Map Method Example

So, this way you can easily convert a python list into a string. I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

Leave a Reply

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