How to do string concatenation without '+' operator in Python?


In this article, we are going to find out how to do string concatenation without plus operator in Python.

The first technique is utilizing the string inbuilt library's join() method. To mix strings with a separator, use this approach. This method produces a string sequence as output.

Python's join() method offers the ability to combine iterable components that have been separated by a string operator. To return a string of iterators with the sequence's items connected by a string separator, use the built-in Python join function.

Example 1

In the example given below, we are taking 2 strings as input and we are concatenating with whitespace using join() method 

str1 = "Welcome"
str2 = "Tutorialspoint"

print("The first string is")
print(str1)

print("The second string is")
print(str2)

concat = " ".join([str1,str2])
print("The concatenated string is")
print(concat)

Output

The output of the above example is as follows −

The first string is
Welcome
The second string is
Tutorialspoint
The concatenated string is
Welcome Tutorialspoint

Example 2

In the program given below, we are taking the same program as above but we are concatenating the input strings with empty space 

str1 = "Welcome"
str2 = "Tutorialspoint"

print("The first string is")
print(str1)

print("The second string is")
print(str2)

concat = "".join([str1,str2])
print("The concatenated string is")
print(concat)

Output

The output of the above example is given below −

The first string is
Welcome
The second string is
Tutorialspoint
The concatenated string is
WelcomeTutorialspoint

Example 3

In the example given below, we are taking the same program as above but we are concatenating using ‘,’ operator 

str1 = "Welcome"
str2 = "Tutorialspoint"

print("The first string is")
print(str1)

print("The second string is")
print(str2)

concat = ",".join([str1,str2])
print("The concatenated string is")
print(concat)

Output

The output of the above example is as shown below −

The first string is
Welcome
The second string is
Tutorialspoint
The concatenated string is
Welcome,Tutorialspoint

Using filter() method

The second technique is to use the format() function from the string library, which is an inherent method. It's mostly used in print statements to include variables. To signal that a certain variable is present, we'll use blossom brackets in double-quotes, and then specify the variable name inside the format() function.

Example

In the example given below, we are taking 2 strings as input and we are concatenating using filter() operation 

str1 = "Welcome"
str2 = "Tutorialspoint"

print("The first string is")
print(str1)

print("The second string is")
print(str2)

concat = "{} {}".format(str1, str2)
print("The concatenated string is")
print(concat)

Output

The output of the above example is as shown below −

The first string is
Welcome
The second string is
Tutorialspoint
The concatenated string is
Welcome Tutorialspoint

Updated on: 07-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements