Skip to content

Convert list to string Python | 4 Example code

  • by

There are many methods to Convert a list to a string in Python. If the list is a string then you can use the do Iterate through the list use join() method or map() method but if the list is of integers, convert the elements before joining them.

Examples convert list to string Python

Simple python example code. Python program to convert a list to string using different methods.

Iterate through the list

def listToStr(s):
    str1 = ""

    for ele in s:
        str1 += ele
    return str1


s = ['A', 'B', 'C']
print(listToStr(s))

Output:

Convert list to string Python

Using .join() method

def listToStr(s):
    str1 = " "
    return (str1.join(s))


s = ['A', 'B', 'C']
print(listToStr(s))

Output: A B C

Using list comprehension


s = ['A', 'B', 'C']
listToStr = ' '.join([str(elem) for elem in s])
print(listToStr)

Output: A B C

Using map()


s = ['A', 'B', 'C']
listToStr = ' '.join(map(str, s))
print(listToStr)

Output: A B C

Get string from int list

If the list is of int (integers), convert the elements before joining them.


list1 = [1, 2, 3]
str1 = ''.join(str(e) for e in list1)
print(str1)

Output: 123

Do comment if you have any doubts and suggestions on this Python list topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading