Python String Concatenation

Python string concatenation means combining two or more strings to obtain a new string. There are many situations where we must combine two or more strings or convert a list into a string. For instance, combine the first name and last name, creating a path from different variables (location, file name, and folder name).

There are multiple ways to perform string concatenation in Python programming language. It includes the most popular f-string, traditional + operator, join() function, format() method, and more. In this article, we will explore all the possible options for performing string concatenation with a practical example.

Python string concatenation examples

The following section shows the possibilities of concatenating multiple strings.

Using the + operator

Using the arithmetic + operator to concatenate two strings is the most straightforward and commonly used option.

In the following example, we declared two string variables and used the + operator to concat those two strings. When we use the arithmetic operator (+) for string concatenation, we must assign the result to a new variable (s).

s1 = "a"
s2 = "b"
s = s1 + s2
print(s)
ab

NOTE: When combining multiple strings, this approach is less efficient.

Adding a space

While doing the Python string concatenation, you should be careful with the separator between two string variables. Python doesn’t provide any separator or delimiter to separate two words. So, when merging multiple strings, you must manually add that extra space.

str1 = 'Tutorial'
str2 = 'Gateway'

str3 = str1 + ' ' + str2
print(str3)

str4 = str1 + ', ' + str2
print(str4)
Tutorial Gateway
Tutorial, Gateway

NOTE: When concatenating different data types, we must convert all variables to a string.

Python String and int or floor Concatenation using str

Unlike most programming languages, it doesn’t implicitly convert an integer to a string to perform concatenation. As you can see, it was throwing an error TypeError: can only on str (not “int”) to str

text = 'Tutorial Gateway'
year = 2018

print(text + year)
Traceback (most recent call last):
.............
TypeError: can only on str (not "int") to str

To resolve this, you have to use the str function to convert the integer or float value year to a str literal.

text = 'Tutorial Gateway '
year = 2026

print(text + str(year))
Tutorial Gateway 2026

Python String Concatenation using join()

The built-in join() function is helpful to concatenate strings (two or more) with a given separator. It is the most commonly used and the best approach to perform string concatenation.

When we are working with a sequence of strings, such as a list of strings or a tuple of items, we use the join() function. We can use the join() method with/without a separator.

In the following example, we use the join() function to concatenate a list of strings. Here, ‘ ‘.join(s) combines all the words in the ‘ s’ list separated by space. Whereas the b =  ‘, ‘.join(s) concatenates the string list, where each word is separated by a comma (CSV).

s = ['Apple', 'Orange', 'Mango', 'Kiwi', 'Cherry']

a = ' '.join(s)
print(a)

b = ', '.join(s)
print(b)

c = ' $ '.join(s)
print(c)
Apple Orange Mango Kiwi Cherry
Apple, Orange, Mango, Kiwi, Cherry
Apple $ Orange $ Mango $ Kiwi $ Cherry

As the join() method takes one argument. If you have different string variables and want to perform string concatenation in Python, we must convert them to a list and apply the join() method on them.

Here, there are four string variables. Next, we convert a list of different words to a list before joining those strings.

s1 = 'Hello '
s2 = 'World '
s3 = 'Welcome '
s4 = 'Guys'

co1 = ' '.join([s1, s2])
print(co1)

co2 = ', '.join([s1, s2, s3])
print(co2)

co3 = ' *+* '.join([s1, s2, s3, s4])
print(co3)
Hello  World
Hello , World , Welcome
Hello  *+* World  *+* Welcome  *+* Guys

Python String Concatenation using a format method

The built-in format function provides an easy way to combine strings. The format function uses curly braces to create placeholders, and these are filled by the format() function’s values.

Here, {} represents the sentence or word, and we can use curly braces based on the total number of strings. You can use any separator, such as spaces or commas, between the {} {} to separate them.

In the following example, the first statement does not have any separator. However, the latter three statements use space, -, and **+** as the separators between two curly braces of the format method. All four statements use the format() method to perform the string concatenation.

str1 = 'Tutorial'
str2 = 'Gateway'

c1 = '{}{}'.format(str1, str2)
print(c1)

c2 = '{} {}'.format(str1, str2)
print(c2)

c3 = '{} - {}'.format(str1, str2)
print(c3)

c4 = '{a} **+** {b}'.format(a=str1, b=str2)
print(c4)
TutorialGateway
Tutorial Gateway
Tutorial - Gateway
Tutorial **+** Gateway

Using f-strings (formatted strings)

Python 3.6 introduced the f-strings concept, which is easy to use and read. We can use the formatted string to perform the string concatenation in Python. We must add f before the concatenation and directly use the string variable inside the curly braces {}.

s1 = "Happy"
s2 = "New Year"

s = f"{s1} {s2}"
print(s)
Happy New Year

Instead of a whitespace, we can also use custom text between the two variables. The best part of the formatted strings is that we can combine multiple data types. In the code below, we combine a string variable, an integer variable, and a custom text.

s1 = "Happy"
s2 = "New Year"
s3 = 2026

s = f"{s1} to be in {s2} party {s3}"
print(s)
Happy to be in New Year party 2026

Using the % operator (old style)

In the Python programming language, you can use the %s format option to perform string concatenation. It is the old style to join two or more strings. However, with the introduction of f-strings, it is less relevant in modern coding.

In the following example, we used the % operator to combine two or more strings. Here, each % represents one value, and we have to specify the format specifier. For instance, use %s for joining strings and for integers, use %d.

str1 = 'Tutorial'
str2 = 'Gateway'
str3 = 'Coding'
year = 2026

c1 = '%s%s' % (str1, str2)
print(c1)

c2 = '%s %s %s' % (str3, str1, str2)
print(c2)

c3 = 'Learn %s at %s %s - Year %d' % (str3, str1, str2, year)
print(c3)

c4 = '%s %s %s %s' % (str1, str2, str3, year)
print(c4)
TutorialGateway
Coding Tutorial Gateway
Learn Coding at Tutorial Gateway - Year 2026
Tutorial Gateway Coding 2026

Python String Concatenation using print() and comma

Placing the words with the print statement separated by a comma will merge or combine strings. For instance, the first print statement in this example concat str1 and str2.

str1 = 'Hello '
str2 = 'World '
str3 = 'Tutorial '
str4 = 'Gateway'
 
print('\nAfter = ', str1, str2)
print()
 
print('After = ', str1, str2, str3)
print()
 
print('After = ', str1, str2, str3, str4)
Python string Concatenation Example

If the requirement is to print the combined text, we can use the print() function. The following example concatenates the list of strings and prints them as the output.

s = ['Apple', 'Orange', 'Mango', 'Kiwi', 'Cherry']
print(*s)
Apple Orange Mango Kiwi Cherry

Python string Concatenation using the += operator

Similar to the arithmetic + operator, we can use the += addition assignment operator to concatenate strings. It simply adds the text from the right side to the left side string.

In the following statement, we used the += Operator, which concatenates str2 to str1 and updates the original string.

str1 = 'Tutorial'
str2 = 'Gateway'

str1 += str2
print(str1)
TutorialGateway

NOTE: As strings are immutable, when we perform the += operation, it destroys or deletes the existing string and recreates the variable with updated text.

Concatenate using the * operator

If you use the * operator along with a numeric value on a single string object, then that sentence is repeated by a given number and returns multiple words. To join the same string multiple times to itself, we can use the * operator.

s1 = 'Hi' * 2
print(s1)

s2 = 'Hi ' * 4
print(s2)
HiHi
Hi Hi Hi Hi

Python string concatenation using 7 methods

In the following example, we use the arithmetic + operator, * operator,  join() function, format() method, f-strings, print() function to perform the string concatenation.

text = 'Hello World '
year = 2026

print(text + str(year))
print(' '.join([text, str(year)]))
print('{}{}'.format(text, year))
print('%s%s' % (text, year))
print(text, year)
print(f'{text}{year}')
print(text * 2)
Hello World 2026
Hello World  2026
Hello World 2026
Hello World 2026
Hello World  2026
Hello World 2026
Hello World Hello World 

This example is the same as the above. However, this time we are allowing the user to enter their own sentences to perform this.

s1 = input('Please enter the First: ')
s2 = input('Please enter the Second: ')

a = s1 + s2
print(a)

b = s1 + ' ' + s2
print(b)

c = ' '.join([s1, s2])
print(c)

print('{} {}'.format(s1, s2))
print('%s %s' % (s1, s2))
print(s1, s2)
print(s1 * 3)

s1 += s2
print(s1)
Please enter the First: Hi
Please enter the Second: hello
Hihello
Hi hello
Hi hello
Hi hello
Hi hello
Hi hello
HiHiHi
Hihello

Which method to choose?

To perform Python string concatenation, we can choose the following methods based on the requirement.

  • + Operator: It is simple, and we can use it to combine two strings.
  • Join(): Use this approach to concatenate a list of strings. When you are combining strings from an object, use this method.
  • f-strings: Most advanced and used in modern programs.
  • format(): When working with the earlier versions, use the format() method.
  • % operator: If the code was written in an earlier version, you see the % format specifier.