Convert List To String In Python

The list is a popular datatype used in Python programming language. The list may contain different types of items like integer, string, list, etc. In some cases, you may need to convert the list or list items into a string. There are different ways to convert a list into a string.

Covert List To String Using for Loop

The simplest way to convert a list into a string is using the for loop where every item in the list will be iterated with the for loop. In every iteration, the current item will be converted into a string and added to the string variable. We will use the str() method which is used to convert any data type into the string type.

mylist = [ 1 , 2 , "poftut" , "ahmet"]

mystr=""

for x in mylist:
   mystr += str(x)

print(mystr)

The output will be like below. As we haven't put any

12poftutahmet

We can use the following method in order to put some delimiters between items when converting them into strings.

mylist = [ 1 , 2 , "poftut" , "ahmet"]

mystr=""

for x in mylist:
   mystr = mystr + "," + str(x)

print(mystr)

Covert List To String Using join() Method

Python string data type provides the join() method which will put given multiple strings into an existing string. The join() method also accepts iterable objects like a list where every item in the list will be converted into a string and then added to the given string. This is more practical than using the for loop iteration where there is no need to create a loop. But this will work only for the lists that only contains string items. If the list contains non-string items it will throw an exception.

mylist = [ "1" , "2" , "poftut" , "ahmet"]

mystr = "".join(mylist)

print(mystr)

The output will be like below.

12poftutahmet

Alternatively we can put a delimiter character between list items while converting them into the string.

mylist = [ "1" , "2" , "poftut" , "ahmet"]

mystr = ",".join(mylist)

print(mystr)

The output will be like below.

1,2,poftut,ahmet

Another alternative way of separating the list items while converting string is setting the delimiter character as a single space like " ". This will make the string more readable.

mylist = [ "1" , "2" , "poftut" , "ahmet"]

mystr = ",".join(mylist)

print(mystr)

The output will be like below.

1 2 poftut ahmet

Covert List To String Using List Comprehension

List comprehension is a method used to extract the list items one by one returning them as a list again. It is like a loop but we can make operations over the items during the comprehension.

mylist = [ 1 , 2 , "poftut" , "ahmet"]

mystr = "".join( [str(x) for x in mylist] )

print(mystr)

The output is like below.

12poftutahmet

We can also used some delimiters between list items like below.

mylist = [ 1 , 2 , "poftut" , "ahmet"]

mystr = ",".join( [str(x) for x in mylist] )

print(mystr)

Covert List To String Using map() Method

The map() method is similar to the list comprehension where given list items will be iterated and joined as a string by using the join() method. In this case, we will provide the str() method as the first parameter for the map() method in order to convert every item of the mylist.

mylist = [ 1 , 2 , "poftut" , "ahmet"]

mystr = "".join( map( str , mylist) )

print(mystr)

Leave a Comment