Skip to content

Python String Concatenation

In Python, we can concatenate strings using the + operator. The + operator allows us to add two or more strings together. Let’s look at some examples.

strings.py
# define strings
string1 = "Hello"
string2 = "World"
# concatenate strings
string3 = string1 + string2
print(string3)

Output:

command
C:\Users\Your Name> python strings.py
HelloWorld

In the above example, we have concatenated two strings using the + operator. The + operator concatenates the second string to the end of the first string.

We can add a space between two strings by adding an empty string ("" or '') to the first string. Let’s look at an example.

strings.py
# define strings
string1 = "Hello"
string2 = "World"
# concatenate strings
string3 = string1 + " " + string2
print(string3)

Output:

command
C:\Users\Your Name> python strings.py
Hello World

In the above example, we have concatenated two strings with a space in between. We have added an empty string ("") with a space in between to the first string.

Concatenate Strings of Different Data Types

Section titled “Concatenate Strings of Different Data Types”

We can concatenate strings of different data types in Python. But we need to convert the non-string data types to strings before concatenating them. We can use the str() function to convert non-string data types to strings. Let’s look at an example.

strings.py
# define strings
string1 = "Hello"
number = 5
# concatenate strings
string2 = string1 + str(number)
print(string2)

Output:

command
C:\Users\Your Name> python strings.py
Hello5

In the above example, we have concatenated a string and an integer. We have converted the integer to a string using the str() function before concatenating it with the string.

We can also concatenate strings using the join() method. The join() method takes an iterable as an argument and concatenates the elements of the iterable to the string. Let’s look at an example.

strings.py
# define strings
string1 = "Hello"
string2 = "World"
# concatenate strings
string3 = "".join([string1, string2])
print(string3)

Output:

command
C:\Users\Your Name> python strings.py
HelloWorld

In the above example, we have concatenated two strings using the join() method. We have passed a list of strings to the join() method. The join() method has concatenated the strings in the list and returned the concatenated string.

sketch Building a string: join wins, and one stray reference wrecks everything p5.js
Three ways to build the same 100,000-character string. join is the fastest because it allocates once. Plain += is not the disaster folklore claims -- CPython can often resize the string in place -- but that optimisation needs the string to have exactly one reference, and keeping a second one anywhere in the loop switches it off. Then every += copies the whole string and the cost becomes quadratic. Measured: 8.90 ms, 3.52 ms, and 114.38 ms.

We can also concatenate strings using f-strings. f-strings are string literals that have an f at the beginning and curly braces ({}) containing expressions that will be replaced with their values. Let’s look at an example.

strings.py
# define strings
string1 = "Hello"
string2 = "World"
# concatenate strings
string3 = f"{string1} {string2}"
print(string3)

Output:

command
C:\Users\Your Name> python strings.py
Hello World

In the above example, we have concatenated two strings using f-strings. We have used curly braces ({}) to insert the values of the variables into the string.

We can also concatenate strings using the % operator. The % operator is also known as the string formatting operator. Let’s look at an example.

strings.py
# define strings
string1 = "Hello"
string2 = "World"
# concatenate strings
string3 = "%s %s" % (string1, string2)
print(string3)

Output:

command
C:\Users\Your Name> python strings.py
Hello World

In the above example, we have concatenated two strings using the % operator. We have used %s to insert the values of the variables into the string.

We can also concatenate strings using string slicing. Let’s look at an example.

strings.py
# define strings
string1 = "Hello"
string2 = "World"
# concatenate strings
string3 = string1[:2] + string2[2:]
print(string3)

Output:

command
C:\Users\Your Name> python strings.py
HeloWorld

We can also concatenate strings in a loop. Let’s look at an example.

strings.py
# define strings
string1 = "Hello"
string2 = "World"
# concatenate strings
string3 = ""
for i in range(5):
    string3 += string1 + string2
print(string3)

Output:

command
C:\Users\Your Name> python strings.py
HelloWorldHelloWorldHelloWorldHelloWorldHelloWorld

In the above example, we have concatenated two strings in a loop. We have used the += operator to concatenate the strings in the loop.

We can also concatenate multiline strings in Python. Let’s look at an example.

strings.py
# define strings
string1 = """Hello
World"""
string2 = """Hello
World"""
# concatenate strings
string3 = string1 + string2
print(string3)

Output:

command
C:\Users\Your Name> python strings.py
Hello
WorldHello
World

We can also concatenate multiple strings in Python. Let’s look at an example.

strings.py
# define strings
string1 = "Hello"
string2 = "World"
# concatenate strings
string3 = string1 * 3 + string2 * 2
print(string3)

Output:

command
C:\Users\Your Name> python strings.py
HelloHelloHelloWorldWorld

In the above example, we have concatenated multiple strings. We have used the * operator to concatenate the strings.

In this tutorial, we have learned how to concatenate strings in Python. We have also learned how to concatenate strings of different data types in Python. Now you can solve problems that require string concatenation in Python. 🎉

Section titled “In this tutorial, we have learned how to concatenate strings in Python. We have also learned how to concatenate strings of different data types in Python. Now you can solve problems that require string concatenation in Python. 🎉”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading