Introduction
String Concatenation is a very common operation in programming. Python string concatenation can be done using various methods. The aim of this tutorial is to explore the different ways to concatenate strings in a Python program.
String concatenation using the + operator
This is the simplest way to concatenate strings. Let's look at a simple example.
s1 = 'Apple'
s2 = 'Pie'
s3 = 'Sauce'
s4 = s1 + s2 + s3
print(s4)Output: ApplePieSauce Let's look at another example where we receive two strings from user input and concatenate them.
s1 = input('Please enter the first string:\n')
s2 = input('Please enter the second string:\n')
print('Concatenated String =', s1 + s2)Output:
Please enter the first string:
Hello
Please enter the second string:
World
Concatenated String = HelloWorldIt's very easy to use the + operator to concatenate strings. However, the arguments must be a string.
>>>'Hello' + 4
Traceback (most recent call last):
File "<input>", line 1, in
TypeError: can only concatenate str (not "int") to strWe can use the str() function to get the string representation of an object. Let's see how to concatenate a string to an integer or another object.
print('Hello' + str(4))
class Data:
id = 0
def __init__(self, i):
self.id = i
def __str__(self):
return 'Data[' + str(self.id) + ']'
print('Hello ' + str(Data(10)))Output:
Hello4
Hello Data[10]The biggest problem with the + operator is that we cannot add a separator or delimiter between strings. For example, if we need to concatenate “Hello” and “World” with a whitespace separator, we need to write it as ""Hello" + " " + "World"" Let's write.
Joining strings using the join() function
We can use the join() function to join strings with a separator. This is useful when we have a sequence of strings, for example a list or multiple strings of strings. If you don't want a separator, use the join() function with an empty string.
s1 = 'Hello'
s2 = 'World'
print('Concatenated String using join() =', "".join([s1, s2]))
print('Concatenated String using join() and whitespaces =', " ".join([s1, s2]))Output:
Concatenated String using join() = HelloWorld
Concatenated String using join() and spaces = Hello WorldConcatenate strings using the % operator
We can use the % operator to format strings, it can also be used to concatenate strings. It is useful when we want to concatenate strings and do simple formatting.
s1 = 'Hello'
s2 = 'World'
s3 = "%s %s" % (s1, s2)
print('String Concatenation using % Operator =', s3)
s3 = "%s %s from JournalDev - %d" % (s1, s2, 2018)
print('String Concatenation using % Operator with Formatting =', s3)Output:
String Concatenation using % Operator = Hello World
String Concatenation using % Operator with Formatting = Hello World from JournalDev - 2018Concatenate strings using the format() function
We can also use the string format() function to concatenate strings and format them.
s1 = 'Hello'
s2 = 'World'
s3 = "{}-{}".format(s1, s2)
print('String Concatenation using format() =', s3)
s3 = "{in1} {in2}".format(in1=s1, in2=s2)
print('String Concatenation using format() =', s3)Output:
String Concatenation using format() = Hello-World
String Concatenation using format() = Hello WorldThe Python String format() function is very powerful, using it just to concatenate strings is not the right use of it.
String concatenation using string f
If you are using Python 3.6+, you can also use f-string to concatenate strings. This is a new way to format strings and was introduced in PEP 498 – Literal String Interpolation.
s1 = 'Hello'
s2 = 'World'
s3 = f'{s1} {s2}'
print('String Concatenation using f-string =', s3)
name = 'Pankaj'
age = 34
d = Data(10)
print(f'{name} age is {age} and d={d}')Output:
String Concatenation using f-string = Hello World
Pankaj age is 34 and d=Data[10]Python's f string is cleaner and easier to write than the format() function. It also calls the str() function when the object argument is used as a field replacement.
Result
Python string formatting can be done in various ways. Use them according to your needs. If you need to join a sequence of strings with a delimiter, use the join() function. If some formatting is also required with the concatenation, use the format() or f-string function. Note that f-string can be used with Python versions 3.6 or later.









