Python concatenate a list of strings | Example code

Use the string join() function to concatenate a list of strings in Python. The join method is faster because it allocates memory only once.

Example concatenate a list of strings in Python

Simple example code joins the list of strings with space.

sentence = ['This', 'is', 'a', 'sentence']

res = " ".join(sentence)
print(res)

Output:

Python concatenate a list of strings

More examples – Python concatenate a list of strings with a separator

Using comma and hyphen as a string separator.

list_abc = ['aaa', 'bbb', 'ccc']

string = ','.join(list_abc)
print(string)

string = '-'.join(list_abc)
print(string)

string = '\n'.join(list_abc)
print(string)

Output:

aaa,bbb,ccc
aaa-bbb-ccc
aaa
bbb
ccc

Do comment if you have any doubts and suggestions on this Python concatenate 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.