Python | Appending text at the end of the string using += Operator

Here, we will learn how to append text at the end of the string using += operator in Python? By IncludeHelp Last updated : February 12, 2024

Problem statement

Given a string, and we have to append more string (text) at the end of the string using += operator in Python.

Appending text at the end of the string

To append text at the end of the string, you can use the augmented addition operator (combination of two operators + and = i.e., +=). The += operator concatenates (appends) text at the end of the string.

Example

Input:
str: 'New Delhi'

Concatenation of more text 
str += ' ' #space
str += 'Chennai'

str += ' ' #space 
str += 'Mumbai'

str += ' ' #space 
str += 'Bangalore'

Output:
str: 'New Delhi Chennai Mumbai Bangalore'

Python program to append text at the end of the string

The below example appends some text at the end of a given string:

# Python program to add strings
# to the string

# Declaring a string object
str = "Hello, World!"

# Printing before append
print("Before appending")
print(str)

# Append using += operatir
str += " How're You?"

# Printing after append
print("After appending")
print(str)

Output

The output of the above program is:

Before appending
Hello, World!
After appending
Hello, World! How're You?

To understand the above example, you should have the basic knowledge of the following Python topics:

Python String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.