In this short tutorial, we look at the different methods that can be used to convert a python list to string.
This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.
Table of Content
- Why convert python list to string?
- Methods to convert python list to strings
- Closing thoughts and recommendations
Why convert python list to string?
Converting python list to string is a common occurrence, although the most common use case I've come across is converting an iterable into a string so that it could be displayed. Python provides us with multiple ways to convert python lists to string, these methods do not have specific limitations for comparison and hence it only boils down to the programmers' understanding and comfort with the method.
With that out of the way, I would recommend this solution in case you are a beginner. But feel free to go through all of them if you are interested to learn them.
Methods to convert python list to strings
Using join():
The most common and pythonic way of converting python list to string is by using the join()
method. In fact, the join()
methods exist to facilitate the same. It takes in iterables join them and returns them as a string. However, the values in the iterable should be of string
data type, and in case you iterable contains int
you can use the second method.
Syntax of Join():
string.join(iterable)
Here string
refers to the desired separator
Parameter:
iterable
- Any iterable - list, tuples, set etc.
Code to convert python list to string using join():
flexiple = ["Hire", "the", "top", "freelancers"]
print(" ".join(flexiple))
#Output - "Hire the top freelancers"
As the separator was a space ( ")the string contains the characters in the list separated by a string.
As aforementioned trying to use join
on an iterable containing an int
would return a typeerror
. The next solution will show you how to bypass this.
flexiple = ["Hire", "the", "top", 10, "python","freelancers"]
print(" ".join(flexiple))
Using join() and map():
This method to convert python list to string using map()
also used a join()
but this method is used when the iterable you are dealing with contains int
values. As the join()
methods only takes in string values, we use the map()
to convert the int
values into a string before converting python list to a string. The map()
methods execute a specific function for all values in an iterable.
Syntax of map():
map(function, iterables)
Parameter:
function
- A specific function that you wish to execute
iterable
- An iterable object containing the values
So by passing the str()
function which converts objects to a string we are able to convert int values and then join them into a string.
Code to convert python list to string using map():
flexiple = ["Hire", "the", "top", 10, "python","freelancers"]
print(" ".join(map(str,flexiple)))
#Output - "Hire the top 10 python freelancers"
Using a loop:
The third methods to convert python list to string is to write a loop and add each iteration to a string. I would suggest this method in case you are new to python and not familiar with concepts such as join()
, map()
etc. The code may be longer but it would be more readable for a beginner.
flexiple = ["Hire", "the", "top", 10, "python","freelancers"]
f1 = ""
for i in flexiple:
f1 += str(i)+ " "
print(f1)
#Output = "Hire the top 10 python freelancers "
Closing thoughts and recommendations
Converting python list to string would most likely not be a one time process and best practice would be to define a function that returns the output. And like I mentioned there aren't significant limitation that can be used to weigh you could choose a methods based on your comfort with the topic. Once you are comfortable with using them try implementing them in a list comprehension.
Do let me know your thoughts in the comment section below. :)
Top comments (2)
Nice read. I thought I knew everything about converting lists to strings, but I learned something new.
Good read.
I've also written a detailed guide on different methods available to convert list to string on Python.
Read: stackvidhya.com/python-list-to-str...