How to convert list to string python?

In this short tutorial, we look at the different methods that can be used to convert a python list to string.

Table of Contents


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 take 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 bypassing 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 method 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 "


As you can see from the output the python continues to skip the iteration for "p" and executes the rest of the code. And similar to the break statement continue can also be used with both for and while loops.

Closing thoughts and recommendations:

Converting python list to string would most likely not be a one-time process and the best practice would be to define a function that returns the output. And like I mentioned there aren't significant limitations that can be used to weigh you could choose a method based on your comfort with the topic. Once you are comfortable with using them try implementing them in a list comprehension.