String Concatenation in Python – Example Tutorial

String Concatenation in Python - Example Tutorial

String concatenation in Python is the process of joining two or more strings together. So with the process of concatenation, you can join the two different strings together and also the same string multiple times.

We will use +, %, join(), and format() functions and operators to concatenate the strings. The + operator joins two strings together. The % operator is used to insert a string value into another string value.

And join() function returns the sequence of elements that have been joined. The format() let us concatenate strings within a string through positional formatting.

So in this tutorial, We will learn about how to concatenate two strings together in python using these operators and functions.

Let’s get started on string concatenation in python using some examples.

Using + Operator

The + operator can be used to join the two different strings together. It is very easy to use this operator and you can call it a python string concatenate operator.

You can use this + operator to combine the multiple strings together, you just have to place this operator between each string.

Note: Make sure all the elements are string type otherwise it will throw an error.

Hello World!
Let’s try to concatenate string and integer together and see what happens.

As I said python will not let you concatenate string and integer together using the + operator. All the elements must have a string type.

But If you try to combine one string and one integer then python will return the error message about the type of the element. Let’s see the following example of string and integer concatenation.

Traceback (most recent call last):
  File "./", line 4, in 
TypeError: can only concatenate str (not "int") to str

As you can see in the above example, we take one string data type and second integer data type value and when we tried to concatenate these two different data types then it return the error message.

But you can fix this error by using the str() function. It will convert your integer data type to string data type and then you can combine them.

Now, when you run the above example code it will print the same output as shown for the first example above. Because we used the str() method and convert the integer to string.

Using % Operator

The % operator is basically used for string formatting but you can also use this operator for string concatenation. It will add a string into another string.

This operator is normally used when you want to concatenate strings together and perform a simple formatting output. You can call this operator name as Python string interpolation.

Hello World!

Using the % operator, you can combine the two different data type variables together like string, integer, float, array, etc.

Using join() Method

The join() function is a string method that returns a string in which an str separator has been used to combine the elements of a sequence.

In the join() method, you have to pass the list of string because the join() method only accept the python list and its arguments.

Hello World!

Using format() Method

The format() is a string formatting function in Python that allows for multiple substitutions and formatting of values. You can use this method to concatenate strings within a string through positional formatting.

The format() function combines the string using the curly braces {} placeholder to set the position of strings. You can add as many curly braces according to the number of strings variables.

Hello World!

Using Comma ,

You can also use the comma (,) mark for string concatenation in python, It is an alternative to the + operator. Use it in the print() method. See the following example code.

Using * Operator

The * operator is an arithmetic operator in python. But if you want to append the same string multiple times then you can use it.

It will append the string and concatenate them and you can define the number of appending strings, which means how many times you want to append the same string. See the following code reference.

Hello Hello Hello Hello Hello 


So in this tutorial, you learned how to string concatenation works in python and how you can concatenate two different strings in multiple ways.

And you see what happens if we concatenate a string and a number because python does not allow combining the two different data types. If you want to concatenate string and integer and first you have to make an integer data type to string data type.

That’s it!!!

If you have any queries please do let me know in the comment section, I’ll respond to you as soon as possible.

You May Also Like

About the Author: Aman Mehra

Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+ years of experience. I love coding and help to people with this blog.

Leave a Reply

Your email address will not be published.