Python Tutorial
Python String Concatenation Explained with Examples
How to Concatenate Strings in Python?
When you concatenate two or more strings into one, this process is called concatenation.
The +
operator in Python does this. It's as easy as writing two-string literals together to concatenate them. A string can be made to repeat for a defined number of times by using the *
operator.
Example:
str1 = 'Hello'
str2 ='World!'
# using +
print('str1 + str2 = ', str1 + str2)
# using *
print('str1 * 3 =', str1 * 3)
Output:
str1 + str2 = HelloWorld!
str1 * 3 = HelloHelloHello
Writing two string literals together also concatenates them like + operator.
If we want to concatenate strings in different lines, we can use parentheses.
>>> # two string literals together
>>> 'Hello ''World!'
'Hello World!'
>>> # using parentheses
>>> s = ('Hello '
... 'World')
>>> s
'Hello World'