Python string concatenation is the process of merging two or more strings. The + operator adds a string to another string. % lets you insert a string into another string value. Both operators are used to concatenate strings in Python.
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.
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.
How to Concatenate Strings in Python
You can concatenate strings in Python using the + and % operators. The + operator adds a value to the end of a string whereas the % operator adds a value to any position in a Python string.
Python Concatenate Strings Using +
The + operator lets you combine two or more strings in Python. This operator is referred to as the Python string concatenation operator. The + operator should appear between the two strings you want to merge.
The syntax for the + operator is:
print("Hello " + "World")
81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.
The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.
Notice that we added a space after the word “Hello “. This is because concatenation does not automatically add a space between the strings we merge.
Optionally, we could assign our strings to string variables. This is useful if you are working with multiple strings that you want to merge.
The + operator returns a new string. This is because strings are immutable. which means they cannot be changed. Any string method in Python returns a new string, no matter what format method you use. The string that is created can be assigned to a new variable.
Concatenate Strings Python with + Example
Let’s say that you are working for the payroll department at a company. 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 white space to the end of our first strings (“Name: “ and “Address: “). This is not done by default, as we discussed earlier.
Converting a Value to an Integer
The + string operator can only be used to merge two string values. 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
You can learn more about how this error works and why it is raised in our “Python typeerror: can only concatenate str (not “int”) to str Solution” article.
We can fix this error by using the str() method to convert the integer value to a string. Our code will return the intended output:
print("John" + str(22))
Our code returns: John22.
Concatenate Strings Python Using “%”
The % string formatting operator merges a string into another string. This operation is commonly called Python string interpolation. You can add a string to any position in an existing string using interpolation.
Whereas the + operator adds a value to the end of a string, the % operator can add a value to at position that you specify.
Let’s look at the syntax for the % operator:
print("%s %s" % ("Hello", "World"))
This code creates a single string with the contents “Hello World”. The %s values represent a value from the tuple that appears after our string. We specify the values that should appear in the string after the % which follows our string.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
We did not use specify a space in any of the values to add to our string. This is because we separated each %s value with a space in our main string.
Let’s use an example to illustrate how the % operator works.
% Operator Example
Say that we have two names that we want to appear in a 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:
Jack and Jill went up the hill.
We use the % operator to declare where we want a value to appear in our string.
We use %a to say that we want a specific value to appear in that position. %b tells our program 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.
We have told our code that we want Jack to be placed in the first open place in our code. Jill should appear in 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.
To learn more about Python, read our complete How to Learn Python guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.