String Concatenation in Python
Introduction
String concatenation in Python means generating new strings by merging one or more strings. Almost all programming languages support string concatenation, and so does Python. In fact, there are multiple ways in Python using which you can concatenate strings. In this tutorial, you will look at all the popular methods that you can leverage to concatenate strings in Python
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 a list!
Method | Category |
f-strings | Most readable |
+ operator | Fastest |
%s operator | Backward compatible |
* operator | Best 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
|
This method also works with the strings assigned to variables. Let’s see how this looks:
# Using + to Concatenate Strings in Python
|
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 ------------------------------------------------------------------------------------------------ |
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 +=, 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 ------------------------------------------------------------------------------------------------ |
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-string, 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 ------------------------------------------------------------------------------------------------ |
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 ------------------------------------------------------------------------------------------------ Output: |
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()
Output: |
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 ------------------------------------------------------------------------------------------------ |
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 ------------------------------------------------------------------------------------------------ |
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 ------------------------------------------------------------------------------- Output: |
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!