Convert list to string Python | 4 Example code

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.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

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