ID : 91
viewed : 95
Tags : PythonPython String
94
In this tutorial, we will discuss methods to append one string to another in Python.
+
Operator in PythonThe , also known as the string concatenation operator, is used to append one string to another. The +
operator returns a string that combines the two strings on both sides of the +
operator. The following code example shows us how we can append one string to another with the +
operator in Python.
str1 = "One" str2 = "Two" str1 += str2 print(str1)
Output:
OneTwo
In the above code, we first initialize two strings, str1
and str2
. We append str2
to the end of str1
using the +
operator.
str.join()
Method in PythonThe can also be used to append one string to another. The str.join()
method returns a string value that combines the calling string and the string passes in the arguments. The following code example shows us how we can append one string to another with the str.join()
method in Python.
str1 = "One" str2 = "Two" str1 = "".join((str1,str2)) print(str1)
Output:
OneTwo
In the above code, we first initialize two strings, str1
and str2
. We append str2
to the end of str1
using the str.join()
method. The arguments of the str.join()
method are enclosed in ()
because the str.join()
method only takes one argument.