Append One String to Another in Python

  1. Append One String to Another With the + Operator in Python
  2. Append One String to Another With the str.join() Method in Python

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 + operator, 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 str.join() method 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.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python String

  • Capitalize First Letter of Each Word in Python
  • Create a Multi Line String in Python