Skip to content

Python concatenate a list of strings | Example code

  • by

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

A 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 examplesPython concatenates a list of strings with a separator

Using commas and hyphens 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

Using a loop:

my_list = ['Hello', ' ', 'World', '!']
result = ''
for element in my_list:
    result += element
print(result)  # Output: Hello World!

Comment if you have any doubts or suggestions on this Python concatenate 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