Python provides many different ways to convert a python list to string. Modular like using python join function and classic which are also known as programmatic approaches. let check all one by one.

Python list to string using python join function

Even while using join function we have many options to do:

Convert a list of characters to string

>>>list1 = ['a', 'b', 'c']
>>>str1 = ''.join(list1)
>>>print(str1)
abc

Previous example has a very simple implementation of python join function usage but using join function we can create a python string with a defined separator, which can be any character available e.g

A comma separated string

>>>list1 = ['x', 'y', 'z']
>>>str1 = ','.join(list1)
>>>print(str1)
x,y,z

A dash character separated string

>>>list1 = ['x', 'y', 'z']
>>>str1 = '-'.join(list1)
>>>print(str1)
x-y-z

In simple words you can use any character to separate the each element of python list.

Convert a list of strings to string

To convert python list to string, python Join function will work in the same manner even in the case of ‘list of strings’

>>>list = ["Geeks", "for", "Geeks"]
>>>str1 = ' '.join(list1)
>>>print(str1)
Geeks for Geeks

Convert a list of characters and integers to string

>>>list1 = [1,2,4]
>>>str1 = ' '.join(list1)

>>>list1 = ['a', 'b', 3]
>>>str2 = ' '.join(list1)

Both of the example snippets to convert python list to string are problematic for python interpreter because python join function can work only str data type. so first make/convert these elements as str(using python str() function) and then apply join method.

To convert all the elements to desired data type(str), python has multiple way arounds.

Python loop has correct code

>>>list1 = [1,2,4]
>>>str1 = " ".join(str(x) for x in list1)
>>>print(str1)
1 2 4

>>>list2 = ['a', 'b', 3]
>>>str2 = ",".join(str(x) for x in list2)
>>>print(str2)
a,b,3

Python map function has correct code

>>>list1 = [1,2,4]
>>>str1 = ' '.join(map(str, list1))
>>>print(str1)
1 2 4

>>>list2 = ['a', 'b', 3]
>>>str2 = ",".join(str(x) for x in list2)
>>>print(str2)
a,b,3

Using a programmatic approach

Now we will see another way to convert a python list to string, a more classical or programmatic approach.


>>>list1 = [1,2,4]
>>>str1  = str(list1)[1:-1]  
>>>print(str1)
1,2,4

>>>list2 = ['a', 'b', 3]
>>>str2 = str(list2)[1:-1]
>>>print(str2)
'a', 'b', 3

How it works: Python str() function transform the list into a string but with its square brackets too. Python string is very similar to python list, as it has indexes start from 0 and the total number of indexes equal to the total number of characters which you can find using python len() function or in detail read python length of list, this code will skip the first(0) index character ‘[‘ and the last(length-1) index character ‘]’, and take all the remaining characters. So every character will be remains same as it is.

Very similar to the upper one

>>> list_of_ints = [80, 443, 8080, 8081]
>>> print str(list_of_ints).strip('[]')
80, 443, 8080, 8081

Even these approach doesn’t have a very modular output but in case if the requirement is to only remove the brackets it will be the best solution.

 

References:

*https://www.decalage.info/en/python/print_list

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.