Skip to content

How to Concatenate Strings in Python: A Complete Guide

How to Concatenate Strings in Python A Complete Guide Cover Image

In this tutorial, you’ll learn how to use Python to concatenate strings. Being able to work with strings in Python is an important skill in almost every program you’ll write. In some cases, you’ll want to display text in your program and need to ensure code readability. In other cases, you will want to aim for speed on your program and your method for concatenating strings will be different. Similarly, if your code requires backward compatibility, you’ll want to use one method over another.

By the end of this tutorial, you’ll have learned:

  • Different ways of concatenating strings in Python
  • How to ensure backward compatibility in concatenating strings in Python
  • What the most readable method of concatenating strings is in Python

Concatenating Strings in Python: Methods Compared

The table below breaks down the different methods of concatenating strings in Python. Each of the methods is covered in more detail in the sections below, as well as some methods that didn’t make the list!

MethodCategory
f-stringsMost readable
+ operatorFastest
%s operatorBackward compatible
* operatorBest to repeat lists
Different methods compared to concatenate strings

Concatenating Strings in Python Using the + Operator

One of the simplest and most common methods of concatenating strings in Python is to use the + operator. The way that this works is that using the + operator joins two strings together. In the case of strings, the + operator acts as the concatenation operator.

Let’s take a look at an example:

# Using + to Concatenate Strings in Python
print('Welcome to ' + 'datagy')

# Returns:
# Welcome to datagy

This method also works with the strings assigned to variables. Let’s see how this looks:

# Using + to Concatenate Strings in Python
first = 'Welcome to '
second = 'datagy'

print(first+second)

# Returns:
# Welcome to datagy

It’s important to note here that this only works with two strings. If we were to try to concatenate a string and an integer, for example, an error would be thrown:

# Trying to Concatenate a String and an Integer
print('Hello!' + 3)

# Raises:
# TypeError: can only concatenate str (not "int") to str

As shown in this tutorial, we first need to convert the integer to a string.

In the next section, you’ll learn how to use the augmented assignment operator to easily append a string to another string.

Concatenating Strings in Python Using the += Operator

In the previous section, you learned how to concatenate strings using the + operator. Similarly, you can use the augmented assignment operator, +=, to concatenate strings. In fact, this operator is great in order to append one string to another.

In this case, it’s important to note that you’re appending a string to the same variable, rather than creating a new one.

# Using += to Concatenate Strings in Python
sentence = 'Welcome to '
second = 'datagy'

sentence += second
print(sentence)

# Returns:
# Welcome to datagy

Keep in mind that while it looks like the string is being added to the end of the other, this isn’t actually the case. Because Python strings are immutable, the original string is actually destroyed and the joined strings are recreated.

Concatenating Strings in Python Using f-Strings

Python f-strings, or formatted string literals, allow you to write clear string interpolation. Python f-strings were first introduced in Python 3.6, so they’re not the most backward compatible code to use to join strings in Python.

Python f-strings are prefixed by the letter f and allow you to place variables to interpolate into square brackets. The variables are evaluated at run-time, and their string representation is placed into the string.

This is best explained using an example, which we can explore below:

# Using Python f-strings to Concatenate Strings
website = 'datagy'
print(f'Welcome to {website}!')

# Returns:
# Welcome to datagy!

The great thing about this is how readable the code is. There is little guesswork in what your final string should look like.

Concatenating Strings in Python Using the * Operator

Another great way to concatenate strings is using the * operator. As the operator implies, you’re multiplying a string a certain number of times. This allows you to a join a string to itself a given number of times.

Let’s see what this looks like:

# Multipling a String
word = 'hello'

print(word * 3)

# Returns:
# hellohellohello

In the code above, we printed out the string 'hello' three times.

Concatenating String Literals in Python Using join Method

The join method takes a list of different strings and, as the name implies, joins the strings together. The method is, itself, applied to a different string that is used to join the strings together. This may sound confusing, but let’s take a look at an example which will explain this better:

# Concatenating Strings with .join()
words = ['welcome', 'to', 'datagy']

joined = ' '.join(words)
print(joined)

# Returns:
# welcome to datagy

In the example above, we apply the .join() method to the string we want to join the items with. Because of this, the string we’re applying the method to is the delimiter that we want to use.

Concatenating Strings in Python Using the format Method

In older versions of Python, curly braces (similar to f-strings) also allowed you to join strings together. However, compared to Python f-strings, this method isn’t as readable. Let’s see how this works by looking at an example:

# Using format to Concatenate Strings
word1 = 'Hey'
word2 = 'there'

words = '{} {}!'.format(word1, word2)

print(words)

# Returns:
Hey there!

This method uses curly braces to specify where different variables should be inserted. The reason that this method isn’t as readable is because it uses position to specify which variable should go where.

Concatenating Strings in Python Using the % Formatting

Similar to the format method, you can use the % operator available in strings to concatenate strings. This method allows you to place variables into strings, albeit in a less readable way.

Let’s take a look at an example:

# Using % to Concatenate Strings
word1 = 'Hey'
word2 = 'there'

words = '%s %s!' % (word1, word2)

print(words)

# Returns:
# Hey there!

Personally, I find this method to be the least readable of the different methods available.

Concatenating Strings in Python Using String Literals

This final method to concatenate strings in Python is to use string literals in succession. By placing one string after another, Python implicitly joins these strings.

Let’s take a look at an example of how this works:

# Joining Strings with String Literals
joined = "hey " "there"

print(joined)

# Returns:
# hey there

Conclusion

In this tutorial, you learned how to concatenate strings in Python. You explored 8 different methods to join strings, taking a look at which are most readable, fastest, and most backward compatible. Because there are so many different ways to consider, it can often be hard to choose. However, knowing which methods to use when can be an important consideration!

Additional Resources

To learn more about related topics, check out the tutorials below:

Leave a Reply

Your email address will not be published.