Using the + operator lets you concatenate characters in Python. This operator is referred to as the Python string concatenation operator
Example concatenate characters in Python
Simple example code using + operator between to characters.
res = 'a' + 'b' + 'c'
print(res)
print(type(res))
Output:
Or
ch1 = "a"
ch2 = "b"
res = ch1 + ch2
print(res)
Output: ab
Alternatively, if you have a collection of characters, such as a list or tuple, you can use the join()
method to concatenate them. Here’s an example:
# Concatenating characters using join()
characters = ['H', 'e', 'l', 'l', 'o']
concatenated = ''.join(characters)
print(concatenated) # Output: Hello
In this case, we create a list characters
containing the individual characters. Then, using the join()
method with an empty string ''
, we concatenate the characters together, resulting in the same output 'Hello'
.
Comment if you have any doubts or suggestions on this Python concatenate 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.