How to Concatenate Strings in Python (in 7 Ways)

This is a comprehensive guide to concatenating strings in Python. This guide demonstrates 7 ways in which you can combine strings. Besides, you’ll learn what approaches are great and what is not so great.

Here’s a short overview of the different approaches:

  1. The + operator
  2. The += operator
  3. The String.join() method
  4. The old % formatter
  5. The String.format() method
  6. The F-strings
  7. Placing strings next to one another

The best approach to concatenating strings is up for debate. In most cases, the most readable and intuitive way will do. This means you should probably use F-strings or the + operator.

Without further ado, let’s jump into the detailed descriptions and examples of each approach.

1. The + Operator

The easiest way to concatenate strings in Python is by using the + operator. Traditionally, you would use the + operator to add numbers together. But in Python, the strings support + operator in concatenating two or more strings.

For example, let’s concatenate four separate strings with the + operator:

a = "This"
b = "is"
c = "a"
d = "test"

combo = a + b + c + d

print(combo)

Output:

Thisisatest

To use the + operator and include spaces between the strings, you need to add them manually between the + operator calls.

For example, let’s add spaces between the strings and concatenate them:

a = "This"
b = "is"
c = "a"
d = "test"

combo = a + " " + b + " " + c + " " + d

print(combo)

Output:

This is a test

But instead of adding a ton of spaces manually, use a loop or another approach down the list.

2. The += Operator

Another easy to way to combine strings is by using the addition assignment operator (+=). Notice that this operator modifies the original string.

Similar to the + operator, using the += operator to concatenate strings is easy, readable, and intuitive. This makes it a great approach for combining strings in Python.

For example, let’s add three strings to the end of an original string:

a = "This"
b = "is"
c = "a"
d = "test"

a += b
a += c
a += d

print(a)

Output:

Thisisatest

To add spaces between the strings, you have to do it manually before adding a new word to the mix.

For example:

a = "This"
b = "is"
c = "a"
d = "test"

a += " "
a += b
a += " "
a += c
a += " "
a += d

print(a)

Output:

This is a test

But as you can tell, it’s not the most elegant way to combine strings if you need spaces. If you need to add spaces, you should consider another approach down the list or use a loop.

3. The join() Method

In Python, strings have a built-in join() method. You can call this method on a string and pass it an array of strings to combine. The join() method uses the string on which it’s called a separator string. It then combines the list of strings passed as arguments.

For example, let’s concatenate four strings and use a blank space as a separator:

a = "This"
b = "is"
c = "a"
d = "test"

combo = " ".join([a, b, c ,d])

print(combo)

Output:

This is a test

4. The % Formatter

One way to concatenate strings in Python is by using formatting.

In the earlier days of Python, using the % operator to format strings was the way to go. The idea is to use % followed by the data type as a placeholder in a string. To embed strings into variables, you can place a %s inside a string. Rather than trying to explain the syntax, it’s best to show examples.

For example, let’s concatenate four strings by using the %s formatter:

a = "This"
b = "is"
c = "a"
d = "test"

combo = "%s %s %s %s" % (a, b, c, d)

print(combo)

Output:

This is a test

Notice that using the % formatting is not the way to go anymore. You should avoid it because there are new more manageable approaches that will be introduced down the list.

5. The format() Method

In Python, you can use the format() method to concatenate strings. Similar to the % operator you learned in the previous section, the format() method is an old way to embed code into strings.

The format() method works such that you place a set of curly braces inside a string. Then you call .format() on the string and place a number of arguments into the format() method call. The format method then embeds the arguments into the {} placeholders inside the string on which it’s called. The number of placeholders must match with the number of arguments.

One way you can use the format() method is to concatenate strings.

For example, let’s combine four strings:

a = "This"
b = "is"
c = "a"
d = "test"

combo = "{} {} {} {}".format(a, b, c, d)

print(combo)

Output:

This is a test

The format() method is the way to go when it comes to formatting strings in older versions of Python (between versions 2.6 – 3.6). But as of Python 3.6, a new even more accessible way to format strings called F-strings was introduced.

6. F-strings

F-strings is the newest approach to formatting strings in Python. You can use F-strings to embed values and code inside a string in a readable and easy fashion. You can use F-strings to concatenate strings by embedding the values inside a string.

An F-string is a string that has the letter “f” before the quotation marks. This turns your string into an F-string which means you can embed values or code inside a string.

Notice that you need Python 3.6+ to use F-strings to format strings.

For example, let’s embed four strings into one string to concatenate the strings into one long string:

a = "This"
b = "is"
c = "a"
d = "test"

combo = f"{a} {b} {c} {d}"

print(combo)

Output:

This is a test

Read also F-strings in Python.

7. Place Strings next to One Another

Last but not least, you can concatenate strings by placing them next to one another. But this is not convenient because you can only do this with strings directly. You cannot do it with string variables, for example.

Here’s an example:

combo = "This" "is" "a" "test"
print(combo)

Output:

Thisisatest

But for the most part, concatenating strings this way is cumbersome. You could write the strings inside one single pair of quotation marks instead of separating them this way.

Summary

Today you learned how to concatenate strings in Python.

To recap, there are 7 different ways to combine strings in Python. What’s the best way to concatenate strings depends on your use case. Unless you’re doing some performance optimizations, choose the most readable and understandable approach!

Nothing is more important than writing manageable quality code that is easy to read and maintain. That’s why the + operator, += operator, and the f-strings are great solutions for grouping strings together.

Thanks for reading. Happy coding!

Read Also