You can join two strings in Python by simply using the ‘+’ operator between them. There are many ways to do it like:-
- Using + operators
- Using join() method
- Using % method
- Using format() function
Python joins two string
A simple example code adds the multiple strings together.
Using + operator
str1 = "Hello "
str2 = "Coder"
# + Operator is used to strings concatenation
str3 = str1 + str2
print(str3)
Output:
Using % operator
We can use the % operator to combine two strings in Python. Its implementation is shown in the below example.
str1 = "Hello"
str2 = "Python"
# % Operator is used here to combine the string
print("% s % s" % (str1, str2))
Using join() method
The join() is a string method that is used to join a sequence of elements.
s1 = "Zero"
s2 = "Point"
s3 = "".join([s1, s2])
s4 = " ".join([s1, s2])
print(s3)
print(s4)
Using format()
The format() is a string formatting function.
s1 = "100"
s2 = "Point"
s3 = "{}{}".format(s1, s2)
s4 = "{} {}".format(s1, s2)
print(s3)
print(s4)
Note: More than two strings can be concatenated using the ‘+’ operator.
Do comment if you have any doubts or suggestions on this Python string topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.