Python - String concatenation

By xngo on June 14, 2019

In any programming language, it is often required to merge or combine strings together. This process is called concatenation. In Python, there are multiple ways to concatenate strings. Here are some examples.

# Concatenate strings using + operator.
firstname = 'Nikki'
lastname  = 'Brown'
fullname = firstname + " " + lastname
print( fullname )
 
# Add strings together using string formatter: {} is the placeholder.
fullname = "{} {}".format(firstname, lastname)
print( fullname )
 
# Concatenate strings using join() function.
tmp_list = [firstname, lastname]
print( ' '.join(tmp_list) )

Output

Nikki Brown
Nikki Brown
Nikki Brown

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.