Python – Append a string to a string variable

Append a string in Python

To append a string value to a string variable in Python, you can use String Addition Assignment Operator. Pass this string variable as left operand, and the other string or another string variable as right operand. The operator appends the other string to the value in this string variable.

thisString += otherString

Examples

1. Append “banana” to value in variable x

In the following program, we take a string value in x. We then use String Addition Assignment operator to append "banana" to the string in x.

Python Program

x = 'apple'
x += 'banana'
print(x)
Run Code Copy

Output

applebanana

2. Append string value in y to value in variable x

In the following program, we take two string values in x, y. We then use String Addition Assignment operator to append the string value in variable y to that of in x.

Python Program

x = 'apple'
y = 'cherry'
x += y
print(output)
Run Code Copy

Output

applecherry

Summary

In this tutorial of Python Examples, we learned how to append another string to the end of this string using String Addition Assignment Operator, with the help of well detailed examples.

Related Tutorials

Code copied to clipboard successfully 👍