Python – Appending strings

Appending Strings in Python

Appending strings in Python is done using the + Addition operator or the += Addition Assignment operator.

1. Appending strings using Addition operator in Python

Addition operator can take two strings as inputs (left operand, and right operand), and append the right operand string to the left operand string.

string1 + string2

The above expression returns a new string created by appending the given strings.

In the following example, we take two strings, in string1 and string2, and append these strings using Addition operator.

Python Program

string1 = "Apple"
string2 = "Banana"

result = string1 + string2
print(result)
Run Code Copy

Output

AppleBanana

We can chain Addition operator, to append multiple strings in a single statement.

In the following program, we append four strings using Addition operator.

Python Program

string1 = "Apple"
string2 = "Banana"
string3 = "Cherry"
string4 = "Mango"

result = string1 + string2 + string3 + string4
print(result)
Run Code Copy

Output

AppleBananaCherryMango

2. Appending strings using Addition Assignment operator in Python

While appending a string string1 to another string string2, you can use Addition Assignment operator += as follows.

string1 += string2

The above expression appends the given strings, and stores the result in string1. The original content in string1 variable would be modified. If this behavior is desired, you may use Addition Assignment operator instead of Addition operator shown in the previous example.

In the following example, we take two strings, in string1 and string2, and append these strings using Addition Assignment operator.

Python Program

string1 = "Apple"
string2 = "Banana"

string1 += string2
print(string1)
Run Code Copy

Output

AppleBanana

3. Appending strings in a List in Python

We append the strings in a given list using join() method of string.

Use the following syntax to join strings of a list my_list, with an empty string as separator.

"".join(my_list)

Use the following syntax to join strings of a list my_list, with a single space as separator.

" ".join(my_list)

Use the following syntax to join strings of a list my_list, with comma as separator.

",".join(my_list)

The above expressions return a new string created by appending the strings in the given list by the specified separator string.

In the following example, we take a list of strings in my_list and append these strings with an empty string as separator between them.

Python Program

my_list = [
    "Apple",
    "Banana",
    "Cherry"
]

result = "".join(my_list)

print(result)
Run Code Copy

Output

AppleBananaCherry

In the following example, we take a list of strings in my_list and append these strings with a comma as separator between them.

Python Program

my_list = [
    "Apple",
    "Banana",
    "Cherry"
]

result = ",".join(my_list)

print(result)
Run Code Copy

Output

Apple,Banana,Cherry

Summary

In this tutorial of Python Strings, we have seen how to append strings in Python, with the help of example programs.

Related Tutorials

Code copied to clipboard successfully 👍