How to append a string with other in Python
In this tutorial, we are going to learn about two different ways to append a string with other in Python with the help of examples.
Using += operator
To append a one string with another string, we can use the +=
operator in Python.
Here is an example:
a = "Hello"
a += "Python"
print(a)
Output:
HelloPython
We can also add a space between two strings like this:
a = "Hello "
a += "Python"
print(a);
Output:
Hello Python
Using string.join() method
Similarly we can also append a string using string.join() method, for that first we need to create a list and append the strings to it, then concantenate the strings using the string.join() method.
Here is an example:
list = []
list.append('Hello')
list.append('Python')
result = "".join(list)
print(result)
Output:
HelloPython