Strings in Python are represented as an array of bytes that contain Unicode characters. Python does not support character data-types. Hence, even a single character is termed as a string of length 1. Since it represented strings in Python as lists, you can access individual characters using indexing. 

Now, several operations are frequently performed on strings in Python. These include inserting or removing characters, computing the length, etc. One other important operation is string concatenation in Python. 

String concatenation in Python means generating new strings by merging one or more strings. Almost all the 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 following are the methods that this article will discuss in detail.

  1. Using the + operator.
    1. Concatenating two or more strings.
    2. Concatenating strings with numbers.
  2. Using the join() method.
  3. Using the % operator.
  4. Using format() function.
    1. Using the default placeholders
    2. Using positional parameters
    3. Using f-strings

So, with no further ado, let’s get started.

Want a Job at AWS? Find Out What It Takes

Cloud Architect Master's ProgramExplore Program
Want a Job at AWS? Find Out What It Takes

1. Using the + Operator for String Concatenation in Python

This is the easiest and efficient way to concatenate strings in Python. However, you need to manually deal with all the spaces, and handle them within the quotes. Let’s check out two possible scenarios.

a. Concatenating Two or More Strings

Now, try to concatenate three strings using the + operator and handle spaces using quotes.

s1 = "Welcome"

s2 = "to"

s3 = "Simplilearn"

merged_string = s1 + " " + s2 + " " + s3

print(merged_string + "!")

In the program above, you used the + operator to concatenate strings along with spaces defined within quotes. You can also use the method inside the print statement to print the concatenated string directly without storing it elsewhere.

StringConcatenation_1

b. Concatenating strings with numbers

Next, try to see what happens if you try to concatenate strings with numbers in Python using the + operator. Consider the program below.

s1 = "Welcome"

s2 = 2

s3 = "Simplilearn"

merged_string = s1 + " " + s2 + " " + s3

print(merged_string + "!")

Here, the second variable is a number instead of a string. Let’s check out the output.

StringConcatenation_2

You can see that it throws a TypeError, stating that it can only concatenate strings with each other and not int data-types. In Python, you can’t concatenate strings with numbers as it is using the + operator. However, to do so, you need to first typecast the number to a string. Let’s do it.

s1 = "Welcome"

s2 = 2

s3 = "Simplilearn"

merged_string = s1 + " " + str(s2) + " " + s3

print(merged_string + "!")

Here, you have converted the second variable to a string and then concatenated it with other strings.

StringConcatenation_3

You can see that the concatenation has now been performed successfully.

2. Using the join() Method for String Concatenation in Python

You can use the join() method to concatenate two or more strings in Python. It also allows you to concatenate strings separated by delimiters. It invokes the join method on a delimiter string, and you need to pass a list of string variables as arguments, and then it returns the concatenated string. If you try to pass any non-string object, it will raise a TypeError. Let’s see how to use it.

s1 = "Welcome"

s2 = "to"

s3 = "Simplilearn"

merged_string = " ".join([s1, s2, s3])

print(merged_string)

StringConcatenation_4

To concatenate integers with strings using the join method, you need to typecast the number into a string, and then pass it into the list of strings. You can use any other delimiter in place of the space as well. For example, if you want to concatenate strings separated by “-”, you should use the following program.

s1 = "Welcome"

s2 = "to"

s3 = "Simplilearn"

merged_string = "-".join([s1, s2, s3])

print(merged_string)

StringConcatenation_5

3. Using the % Operator for String Concatenation in Python

The % operator is used for string formatting in general. You can also use it for string concatenation in Python. However, the % operator is deprecated and will be removed in later versions of Python. Hence, it is recommended that you adopt other methods for string concatenation in Python. Now, check out the below program. 

s1 = "Welcome"

s2 = "to"

s3 = "Simplilearn"

merged_string = "This is a message which says \"%s %s %s\"!"%(s1, s2, s3) 

print(merged_string)

Here, you can see how the demo has used the % operator to create the merged_string variable which concatenates s1, s2, and s3 string variables.

StringConcatenation_6

4. Using the format() Method for String Concatenation in Python

Instead of using the % operator to format and concatenate strings, the format() method provides much more flexibility and is probably a better option. Let’s look at three different use-cases of the format() method.

a. Using the Default Placeholders

In this method, you can simply use an empty set of curly braces wherever you want to concatenate strings and then use the .format() method on the overall string to define the string variables that need to be placed at those positions, in the same order. Let’s understand this with the help of the below program.

s1 = "Welcome"

s2 = "to"

s3 = "Simplilearn"

s4 = "Python"

merged_string = "This is a message which says \"{} to {}!\". This is a {} tutorial.".format(s1, s3, s4)

print(merged_string)

In the program above, you can see how the demo has used curly braces to define the positions of the strings to be placed. And then, inside the .format() method, you have passed the strings as arguments that you want to be placed in that order.

StringConcatenation_7

Take Your Data Scientist Skills to the Next Level

With the Data Scientist Master’s Program from IBMExplore Program
Take Your Data Scientist Skills to the Next Level

b. Using Positional Parameters

Instead of using empty curly braces, you can define the positions inside those curly braces to avoid confusion when you concatenate multiple strings. Consider the example below.

s1 = "Simplilearn"

s2 = "Welcome"

s3 = "Python"

merged_string = "This is a message which says \"{1} to {0}!\". This is a {2} tutorial.".format(s1, s2, s3)

print(merged_string)

Inside the curly braces, you have defined the position of variables that you have passed as arguments in the .format() method.

/StringConcatenation_8

c. Using f-strings 

Python 3.6 and onwards allows you to use f-strings to format or concatenate strings in Python. Instead of leaving the curly braces empty or passing the positions, you can simply pass the variables directly. Let’s check it out.

s1 = "Simplilearn"

s2 = "Welcome"

s3 = "Python"

merged_string = f"This is a message which says \"{s2} to {s1}!\". This is a {s3} tutorial."

print(merged_string)

You can see that we just need to place an “f” before the string, and then you can pass the variables directly inside the curly braces. This is also the recommended way of string formatting for Python 3.6 and above. 

StringConcatenation_9.

Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

Wrapping Up!

To conclude, in this tutorial, you saw multiple methods that can be used for string concatenation in Python. String concatenation is one of the most frequently used operations on strings. You usually use it to print customized messages, format strings and perform a ton of other operations. Hence, it’s very important to find out the most optimal method to concatenate strings that would cater to your requirements.

We hope that with the help of this comprehensive and detailed tutorial, you will now be able to get hands-on with string concatenation in Python.

If you are looking to master the python language and become an expert python developer, Simplilearn’s Python Certification Course is ideal for you. Delivered by world-class instructors, this comprehensive certification course will not only cover the basics of Python, but give you in-depth expertise in key areas such as implementing conditional statements, handling data operations, Django, and shell scripting.

Have any questions for us on this string concatenation in Python article? If yes, leave them in the comments section of this article, and our experts will get back to you at the earliest!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 18 Dec, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449