Concatenate Two Strings in Python : A Complete Tutorial Guide

Published:4 June 2021 - 4 min. read

Shahzaib Kamal Image

Shahzaib Kamal

Read more tutorials by Shahzaib Kamal!

Azure Cloud Labs: these FREE, on‑demand Azure Cloud Labs will get you into a real‑world environment and account, walking you through step‑by‑step how to best protect, secure, and recover Azure data.

String concatenation is a common practice in Python. Like with many other programming languages though, are you sure you’re using the right method? If you’re wondering how to concatenate two strings in Python and more, stay tuned.

In this tutorial, you are going to learn how to perform string concatenation in many unique and possible ways to help you pick the best method for your unique situation.

Read on!

Prerequisites before you Concatenate Two Strings in Python

This tutorial is hands-on and will include many different demos. If you intend to follow along, be sure you have the following:

  • Python v3+ on any operating system. This tutorial will use Python v3.8.4.

The + Operator

One of the most popular methods to concatenate two strings in Python (or more) is using the + operator. The + operator, when used with two strings, concatenates the strings together to form one.

In a Python script, copy, paste and execute the code below. This simple script creates two string variables a and b. It then uses the + operator to merge or concatenate them together to form a single string.

a = "You are "
b = "learning Python"

a + b

The + operator doesn’t just concatenate two strings in Python. You can add as many as you’d like. The below example creates another string variable and adds it to the end of the string.

a = "You are "
b = "learning "
c = "python"

a + b + c

You will see you get the same output for both examples.

The Join() Method

The join() method is another good way to concatenate two strings in Python. Similar to the + operator, the join() method requires at least two strings to join together. The join() method isn’t necessarily meant to concatenate strings, but to join list elements together.

To use the join() method, first create a list of strings. The example below is creating a string to join all list elements with (a space). The join() method is on all string objects in Python so it’s calling the method passing in three string variables defined in a list [a,b,c].

a = "You are"
b = "learning python"
c = "using join method"

" ".join([a, b, c])
Python join method
Python join method

Instead of a space (" ") to join the list elements by, you can use any character(s) you’d like, as shown below.

a = "You are"
b = "learning python"
c = "using join method"

"*".join([a, b, c])

You can see below that the spaces have been replaced by asterisks.

Python join method with asterisks
Python join method with asterisks

The String Formatting % Operator

Python has a concept called string formatting. String formatting is a feature of Python that allows you to dynamically insert strings within strings with placeholders. Typically, most don’t consider using string formatting to concatenate strings but it’s possible.

Let’s say you need to concatenate the three strings you’ve been working with previously. To be more specific, you’d like to concatenate two strings in Python together separated by a space.

To concatenate multiple strings together with the string formatting construct %, first create a string with two placeholders or Python format specifiers representing where each string will be placed ("%s %s"). Each placeholder is represented by %s.

Once you have the intended string, then use the % construct to pass a list of strings into that “placeholder string”, as shown below.

a = "You are"
b = "learning"

"%s %s" % (a, b)

Even if a and b were integers (more later), you would still get the expected string because Python would cast each integer to a string.

a = 1
b = 2

"%s %s" % (a, b)

The Format() Method

You can also use another Python formatting technique with the format() method. Like the % string formatting construct, the format() method allows you to define placeholders in a string and pass one or more strings inside.

Instead of using %s placeholders, you will use curly braces ({}) to represent the location where you’d like to place the strings.

Using the built-in string method format(), the example below creates a string with placeholders ({}) you’d like to insert strings a, b and c into. It then passes the values of each string variable with as arguments to the format() method.

a = "You are"
b = "learning python"
c = "using format method"

"{} {} {}".format(a, b, c)

Once executed, you can see that Python replaces each {} with the value of a, b and c from left to right.

Python format method
Python format method

The “placeholder” string, like using the % construct can be anything as long as you specify the same number of “placeholders” as values you’re passing into it.

a = "You are"
b = "learning python"
c = "using format method"

print("{}*{}*{}".format(a, b, c))
Python format method with asterisks
Python format method with asterisks

The f-string Construct

For the final way to concatenate two strings in Python, check out the f-string construct. Another string formatting technique, f-string allows you to define “placeholders” and then pass in string values to those placeholders.

You can see below the concept is the same as the other string formatting techniques. Only this time, you start the “placeholder string” with an f and enclose the string variables inside of the curly braces within.

a = "You are"
b = "learning python"
c = "using f-string method"

f"{a} {b} {c}"
concatenate two strings in Python via Python f-string method
concatenate two strings in Python via Python f-string method

Python Gotcha: String Type Conversions

When concatenating strings together in Python, pay attention to types. You will come across unexpected behavior if, for example, you intend to concatenate two strings in Python together but use a non-string type instead.

For example, using the + operator, take a look at the following code and the output. Notice that Python returns an error. It returns an error because 3 is an integer and not a string.

a = "You are "
b = "learning "
c = "python "
d = 3
a + b + c + d
Python error when using different data types
Python error when using different data types

If you need to concatenate a different type like an integer, always remember to enclose it with single or double quotes, like below.

a = "You are "
b = "learning "
c = "python "
d = "3"
a + b + c + d

Conclusion

You should now have the proper understanding of how to concatenate two or more strings in Python using many unique methods. Whether it’s intended concatenation methods like the + operator or via string formatting, you should now know how to work with both.

Which method you prefer for your real life Python project?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!