Learn Python – How to concatenate two strings in Python- Basic and advance

Python string is a series of Unicode characters. Python gives many built-in functions for string manipulation. String concatenation is a procedure when one string is merged with every other string. It can be accomplished in the following ways.

Using + operators

Using join() method

Using % method

Using format() function

Let’s recognize the following string concatenation methods.

Using + operator

This is an effortless way to combine the two strings. The + operator adds the more than one strings together. Strings must be assigned to the extraordinary variables because strings are immutable. Let’s recognize the following example.

Example –

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

Output:

Hello Devansh

Explanation:

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

Using join() method

The join() approach is used to be part of the string in which the str separator has joined the sequence elements. Let’s apprehend the following example.

Example –

# Python program to  
# string concatenation  
  
str1 = "Hello"  
str2 = "JavaTpoint"  
  
# 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])  
  
print(str3)  

Output:

HelloJavaTpoint
Hello JavaTpoint

Explanation:

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

Using % Operator

The p.c operator is used for string formatting. It can also be used for string concatenation. Let’s apprehend the following example.

Example –

# Python program to demonstrate  
# string concatenation  
  
str1 = "Hello"  
str2 = "JavaTpoint"  
  
# % Operator is used here to combine the string  
print("% s % s" % (str1, str2))  

Output:

Hello JavaTpoint

Explanation –

In the above code, the %s represents the string data-type. We handed each variables’s value to the %s that combined the strings and back the “Hello JavaTpoint”.

Using format() function

Python gives the str.format() function, which approves use of a couple of substitutions and fee formatting. It accepts the positional arguments and concatenates the string thru positional formatting. Let’s understand the following example.

Example –

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

Output:

Hello JavaTpoint
Hello JavaTpoint

Explanation:

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