Python - How To Append One String to Another in Python

ID : 91

viewed : 95

Tags : PythonPython String

vote vote

94

In this tutorial, we will discuss methods to append one string to another in Python.

Append One String to Another With the + Operator in Python

The , 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.

Append One String to Another With the str.join() Method in Python

The 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.

  • Related HOW TO?