To concatenate strings in Python the + or % operator is used. This will merge multiple strings’ data. The + operator adds one string to the end of another, and the % operator can insert a single string into another string.
When you’re working with a string in Python, you may want to combine it with another string. For example, you may have a user’s first name and surname and want to combine them together to get the user’s full name. Or you may have two supplier names that you want to appear in the same string.
In coding, we call merging strings together string concatenation. Python offers a number of string methods that can be used to concatenate separate strings and return a new one. In this tutorial, we are going to discuss how to use the + and % operators to concatenate strings in Python.
Python String Refresher
Strings are a sequence of one or more characters and are used to store text that can be read and interpreted by a computer. Strings are immutable, and therefore cannot be changed after they have been created. In Python programs, strings exist between either single (
‘’) or double (
“”) quotes. Here’s an example of a Python string:
"This is an example Python string."
You can use either single quotes or double quotes, as long as you use the same quotation mark throughout your program to maintain consistency.
Python Concatenate Strings Using “+”
Combining two or more strings together is a common operation in many programming languages. Concatenation allows you to take two separate strings and combine them into one.
In Python, there are a few ways to concatenate strings. The most common is to use the
+ operator, but you can also use the
% operator, as we will discuss below.
Let’s say that you are working for the payroll department at a company and you want to print out an employee’s contact information to the console. Instead of just printing out the name of the employee and their address, you want to return the information with labels.
You could use string concatenation to merge labels with the employee’s contact information to get your desired output. Here’s an example of a program that merges this information:
print("Name: " + "John") print("Address: " + "San Francisco")
Our program returns the following output:
Name: John Address: San Francisco
As you can see, our two strings on each line have been concatenated with the operator
+. On our first line, the string
Name: John was created, and on the second line, the string
Address: San Francisco was created.
In the above example, we added whitespace to the end of our first strings (
“Name: “ and “Address: “). However, this is not done by default. So, if you want whitespace between the strings you are concatenating, you need to add a space.
It’s also important to note that the
+ string operator can only be used to merge two strings —you cannot combine data of two different data types using concatenation. So, if you have an integer and a string that you want to combine, you’ll need to convert the integer to a string. Here’s what happens if we try to combine a string and an integer:
print("John" + 22)
Our code returns the following error:
TypeError: can only concatenate str (not "int") to str
If we convert our integer to a string using
str(), our code will return the intended output:
print("John" + str(22))
Our code returns:
Python Concatenate Strings Using “%”
Strings can also be concatenated using string interpolation, which is a technique used in string formatting. String interpolation is used to describe a process where you combine two strings together based on a certain set of criteria.
The most common approach to concatenate strings using interpolation is by using the
Let’s use an example to illustrate how the
% operator works. Say that we have two names that we want to appear in a pre-written string. Here’s the code we could use to add those names to our string:
jack_and_jill = "%a and %b went up the hill." % ("Jack", "Jill") print(jack_and_jill)
Our code returns the following:
Jack and Jill went up the hill.
There’s a lot going on here, so let’s break it down.
In our string, we use the
% operator to declare where we want a value to appear in our string.
So, we use
%a to say that we want a specific value to appear in that position, and
%b to say that we want another value to appear in that position. Then, at the end of our string, we use the
% operator and tell our code what values we want to appear in our list.
In this case, we have told our code that we want
Jack to be placed in the first open place in our code, and
Jill to be merged into the second open place. That leaves us with the string
Jack and Jill went up the hill.
String concatenation is a common operation in many programming languages where you combine two or more strings into one. For example, you may want to combine an employee’s first name and surname into one string, or two different flavors of pizza into one.
In this tutorial, we discussed how you can use the
+ operator to concatenate strings in Python. We also explored how you can use the
% operator to interpolate strings in Python. Now you’re equipped with the information you need to start concatenating strings in Python like an expert!
Curious to learn more about Python and why the skill is in-demand in the technology industry? Download the free Career Karma app today and talk with one of our expert career coaches!