How String Concatenation Works in Python


The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

This post will show you how to use the string method concatenation in Python. It explains what it does and its different usages, as well as some examples of its usage.
The following code snippet shows a very basic example:
个age = 25; 品yuan = 50; 歲a= 100 + age ; 英里eiri= 3 * (2/7) * ((1/8);

String concatenation is the process of joining two or more strings together. This can be done in Python using the + operator.

How String Concatenation Works in Python

In Python, string concatenation is a popular behavior. Are you sure you’re using the correct way, as you are with many other programming languages? Stay tuned if you want to learn how to concatenate two strings in Python and more.

In this article, you’ll learn how to concatenate strings in a variety of different methods to help you choose the optimal solution for your specific case.

Read on!


This is a hands-on instruction with a variety of demonstrations. If you want to follow along, you’ll need the following:

  • Any operating system with Python v3+. Python 3.8.4 will be used in this course.

How Do You Install Python 3.6? Related:

The plus sign (+)

One of the most popular methods to concatenate two strings in Python (or more) is using The plus sign (+). The plus sign (+), 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 plus sign (+) to merge or concatenate them together to form a single string.

“You’re studying Python,” says a. a + b

The plus sign (+) doesn’t just concatenate two strings together. 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 studying ” b = “python” c = “python” a + b + c

You’ll see that both instances provide the same results.

The method Join()

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

To use The method Join(), first create a list of strings. The example below is creating a string to join all list elements with (a space). The method Join() 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 = “Python learning” “Using the join technique” c = ” join ([a, b, c])

Join function in PythonJoin function in Python

You may use any character(s) to link the list components instead of a space (” “), as demonstrated below.

a = “You are” b = “Python learning” “*” c = “using join technique” join ([a, b, c])

The spaces have been replaced with asterisks as shown below.

Join function in Python with asterisksJoin function in Python with asterisks

The percent Operator for String Formatting

String formatting is a notion in Python. Python’s string formatting functionality enables you to dynamically insert strings inside strings using placeholders. Most people don’t think about string formatting while concatenating strings, yet it is feasible.

Assume you need to concatenate the three strings you’ve already worked with. To be more clear, you want to concatenate two strings separated by a space in Python.

To use the string formatting construct percent to concatenate several strings, first generate a string with two placeholders or Python format specifiers that specify where each text will be inserted (” percent s percent s”). Percent s are used to symbolize each placeholder.

Use the percent technique to feed a collection of strings into that “placeholder string,” as demonstrated below.

“percent s percent s” percent a = “You are” b = “Learning” (a, b)

Even if a and b were integers (more on that later), the anticipated string would be returned since Python casts each integer to a string.

” percent s percent s” percent a = 1 b = 2 (a, b)

The Format() Procedure

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

Curly braces () will be used instead of percent s placeholders to mark the position where the strings should be placed.

Getting Started with Python Functions for Beginners

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() Procedure.

a = “You are” b = “Python learning” c = “using the format technique” “”. (a, b, c) format

Python replaces each with the value of a, b, and c from left to right once it has been run.

Format method in PythonFormat method in Python

The “placeholder” string, like the percent construct, may be anything as long as the number of “placeholders” is the same as the number of values you’re providing into it.

a = “You are” b = “Python learning” print(“**”.format(a, b, c)) print(“**”.format(a, b, c))

Format method in Python with asterisksFormat method in Python with asterisks

Construct for f-strings

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

The premise is the same as the other string formatting approaches, as you can see below. Only this time, the “placeholder string” begins with a f and the string variables are included inside the curly brackets.

a = “You are” b = “Python learning” f”a b c” c = “using f-string technique”

f-string method in Pythonf-string method in Python

Conversions of String Types in Python

Pay attention to types while concatenating strings in Python. If you want to concatenate two strings but use a non-string type instead, you’ll receive unexpected results.

For example, using The plus sign (+), 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 studying ” b = “python ” c = ” d = 3 a + b + c + d

When utilizing distinct data types in Python, an error occurs.When utilizing distinct data types in Python, an error occurs.

If you need to concatenate a different type, such as an integer, always use single or double quotes, as seen below.

a = “You are studying ” b = “python ” c = “3” d = “3” a + b + c + d


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 plus sign (+) or via string formatting, you should now know how to work with both.

Which approach would you use in a real-world Python project?

In Python, “concatenation” is the process of joining two or more strings together to form a single string. The process involves appending one string onto another. This can be done with the + operator or by using the str.join() function. Reference: concatenation in python example.

Related Tags

  • best way to concatenate strings in python
  • python concatenate string and int
  • python string concatenation with variable
  • how to concatenate two strings in python with space
  • python concatenate r string

Table of Content