How to Append One String to Another in Python

Muhammad Maisam Abbas Feb 02, 2024
  1. Append One String to Another With the + Operator in Python
  2. Append One String to Another With the str.join() Method in Python
How to Append One String to Another 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.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Python String