How to Convert Python List to String

How to Convert Python List to String : In this article we will see how to convert a python list into a string
Overview
In this tutorial we will go through the most efficient methods to convert a list into a string. This conversion is often used in programming, in file processing or in data science. For example, Python provides a join() function that takes a sequence and converts it into a string. But this is not the only way to do it.
We will illustrate these methods with clear and concise examples 🙂
Convert Python List to a string using join() function
the Join() function can easily convert a list into a string, its syntax is as follows :
separator.join(list)
separator is a string. We can put whatever we want as separator like comma, space, hyphen etc.
The list parameter of the join() function must be a sequence (A string, a list, a tuple etc…)
Let’s take the following list as an example:
websiteTitle = ["AMIRA", "DATA", "ALL", "THE", "DATA", "YOU", "NEED"]
To convert this list into a string where each list element is separated by a space we need to execute the following line:
websiteTitle = ["AMIRA", "DATA", "ALL", "THE", "DATA", "YOU", "NEED"]
print(websiteTitle)
print("Variable type : ")
print(type(websiteTitle))
websiteTitle = ' '.join(websiteTitle)
print(websiteTitle)
print("Variable type : ")
print(type(websiteTitle))
Output:
[‘AMIRA’, ‘DATA’, ‘ALL’, ‘THE’, ‘DATA’, ‘YOU’, ‘NEED’]
Variable type :
class ‘list’
AMIRA DATA ALL THE DATA YOU NEED
Variable type :
class ‘str’
This syntax only works for lists that contain string elements.
Using map() function
To convert a list that contains strings as well as integers, we can use the combination of the map() and join() functions
websiteTitle = ["AMIRA", "DATA", 2020, 5, 14]
print(websiteTitle)
websiteTitle = ','.join(map(str,websiteTitle))
print(websiteTitle)
Output:
[‘AMIRA’, ‘DATA’, 2020, 5, 14]
AMIRA,DATA,2020,5,14
We have separated each element of the list with a comma and we can see that the map() function allowed us to convert integer type elements into a string.
Using list comprehension
We can use a loop to browse through each element in the list and convert that element into a string at the same time:
websiteTitle = ["AMIRA", "DATA", 2020, 5, 14]
print(websiteTitle)
websiteTitle = ' '.join([str(elem) for elem in websiteTitle])
print(websiteTitle)
Output:
[‘AMIRA’, ‘DATA’, 2020, 5, 14]
AMIRA DATA 2020 5 14
Python list to string separated by a comma
If you wish to obtain a string of characters separated only by commas, there is a simple way to do so :
websiteTitle = ["AMIRA", "DATA", 2020, 5, 14]
print(str(websiteTitle).strip('[]'))
Output :
‘AMIRA’, ‘DATA’, 2020, 5, 14
It’s not the most elegant way, I’ll grant you that, but it gets the job done. 🙂
Conclusion
We have seen different ways to convert a list into strings. The join() method is the best suited to do this kind of conversion and does it very well at the same time. Feel free to ask questions about using these functions, I will be happy to answer them. 🙂
Comments
Leave a comment