Python concatenate strings: The detailed guide for the beginner

When you’re working with a string in Python, you may want to combine it with another string. Merging strings together calls string concatenation. Python offers a number of string methods that can be used to Python concatenate strings and return a new one.

This tutorial gives you a detail guide, based on documents from Javapoint and Careerkarma, for the beginner when have Python concatenate strings.

How to Concatenate Strings in Python

Python concatenate

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:

1 print("Hello " + "World")

This code concatenates, or merges, the Python strings “Hello ” and “World”. The Python print() statement displays the final string to the console.

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.

Example # Python program to show  

# string concatenation  
# Defining strings  
str1 = "Hello "  
str2 = "ArrowHiTech"  
# + Operator is used to strings concatenation 
str3 = str1 + str2  
print(str3)   # Printing the new combined string  


Hello ArrowHiTech


In the above example, the variable str1 stores the string “Hello”, and variable str2 stores the “ArrowHiTech”. We used the + operator to combine these two string variables and stored in str3.

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:

1 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.

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.

% 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:

1 jack_and_jill = "%a and %b went up the hill." % ("Jack", "Jill")

2 print(jack_and_jill)

Our code returns:

1 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.

Using join() method

The join() method is used to join the string in which the str separator has joined the sequence elements. Let’s understand the following example.


# Python program to  
# string concatenation  
str1 = "Hello" 
str2 = "ArrowHiTech"  
# join() method is used to combine the strings  
print("".join([str1, str2]))  
# join() method is used to combine  
# the string with a separator Space(" ")  
str3 = " ".join([str1, str2])  


Hello ArrowHiTech


In the above code, the variable str1 stores the string “Hello” and variable str2 stores the “ArrowHiTech”. The join() method returns the combined string that is stored in the str1 and str2. The join() method takes only list as an argument.

Using format() function

Python provides the str.format() function, which allows use of multiple substitutions and value formatting. It accepts the positional arguments and concatenates the string through positional formatting. Let’s understand the following example.


# Python program to show   
# string concatenation   
  str1 = "Hello"  
str2 = "ArrowHiTech"  
# format function is used here to   
# concatenate the string   
print("{} {}".format(str1, str2))   
# store the result in another variable   
str3 = "{} {}".format(str1, str2)   


Hello ArrowHiTech
Hello ArrowHiTech


In the above code, the format() function combines both strings and stores into the str3 variable. The curly braces {} are used as the position of the strings.

To sum up with Python concatenate

Python concatenate strings is a common operation in many programming languages where you combine two or more strings into one. Moreover, to learn more about Python, read our complete Python Projects For Beginners: Amazing Ideas And Topics You Should Explore

Any help with website development services? Let’s ArrowHiTech help you!

If you’re considering web development services, you can explore more about us – ArrowHiTech. Above all, we are proud to be one of the most well-established outsourcing companies all around the world. In addition, with over 12 years of experience, we can provide the best Web app development services for your business.

ArrowHiTech services

So, if you have any questions, don’t hesitate to contact us via: