Different Ways of String Concatenation in Python
Method 1: Python String Concatenation using + Operator
The simplest Method is to use the + Operator for the Python string Concatenation. This Operator is used to add multiple strings together. Here, the arguments should be a string. The + Operator joins the string stored in v1 and v2 and stores it in another variable, v3.
Python
# Defining the strings
v1 = "Coding "
v2 = "Ninjas"
# The + Operator used to combine the strings
v3 = v1 + v2
print(v3)

You can also try this code with Online Python Compiler
Run Code
Output:
Coding Ninjas
Method 2: Python String Concatenation using join() Method
The join() method returns a string with a str separator joining the sequence elements. This Method will combine the strings stored in the v1 and v2 variables. This Method only accepts the list as the argument; the list size can be anything.
Python
v1 = "Coding"
v2 = "Ninjas"
# join() method used to combine strings
print("".join([v1, v2]))
# join() method is used to combine
# string with the separator Space(" ")
v3 = " ".join([v1, v2])
print(v3)

You can also try this code with Online Python Compiler
Run Code
Output:
CodingNinjas
Coding Ninjas
Method 3: Python String Concatenation using % Operator
The % operator is used for string formatting. It can be used for string concatenation also. This Operator is useful when we need to concatenate the strings and perform simple formatting. The % denotes string data type. The values in both the variables will be passed to the string %s and become "Coding Ninjas".
Python
v1 = "Coding"
v2 = "Ninjas"
# % Operator used to combine string
print("% s % s" % (v1, v2))

You can also try this code with Online Python Compiler
Run Code
Output:
Coding Ninjas
Method 4: Python String Concatenation using format() function
This Method allows value formatting and multiple substitutions in Python. This Method is used in Python for string formatting as string.format(). This Method concatenates the elements within a string through positional formatting. In this Method, curly braces {} will be used to set the position of the strings. The first curly brace stores the first variable, and the second curly brace stores the second variable. Finally, it will print "Coding Ninjas".
Python
v1 = "Coding"
v2 = "Ninjas"
print("{} {}".format(v1,v2))
# store the output in another variable
v3 = "{} {}".format(v1, v2)
print(v3)

You can also try this code with Online Python Compiler
Run Code
Output:
Coding Ninjas
Coding Ninjas
Method 5: Python String Concatenation using (, comma)
When you need to include only one whitespace, one of the best alternatives to string concatenation using the “+” operator is “,”.
Python
v1 = "Coding"
v2 = "Ninjas"
# combine data types with the single whitespace.
print(v1, v2)

You can also try this code with Online Python Compiler
Run Code
Output:
Coding Ninjas
Method 6: Python String Concatenation using f-string
If you use Python 3.6+, you can easily use the f-string function for string concatenation. This new method to format the strings was introduced in PEP 498 - Literal String Interpolation.
Python
v1 = 'Coding'
v2 = 'Ninjas'
v3 = f'{v1} {v2}'
print('String Concatenation =', v3)

You can also try this code with Online Python Compiler
Run Code
Output:
String Concatenation = Coding Ninjas
You can practice by yourself with the help of online python compiler for better understanding.
Frequently Asked Questions
What is the most efficient way to concatenate strings in Python?
Use the join() method in Python for efficient string concatenation; it creates one string object. In contrast, the + operator generates a new object each time it's used.
How to concatenate two strings?
Using the + operator or the str.join() method, you can concatenate two strings in Python.
What is concat() in Python?
Python does not have a concat() method for strings. String concatenation is typically done using the + operator or join() method.
Can we concatenate three strings in Python?
Yes, you can concatenate three strings in Python using the + operator, join() method, or other string manipulation techniques.
Conclusion
In this article, we have learned Python String Concatenation. Python offers several methods for string concatenation, each with its own advantages depending on the use case. While the + operator is simple and intuitive, it may not always be the most efficient for concatenating multiple strings, especially in loops. Methods like join() provide better performance and are ideal for concatenating large numbers of strings.
You can also refer to the below articles:
You can learn the basics of Java and data structures and algorithms in Java on Code360. Refer to our guided path on code studio to learn Competitive Programming, Javascript System Design, etc. Enroll in our courses and refer to the mock test and problems available. Also, look at the interview experiences for placement preparations.