Home » Python » Python programs

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?
Submitted by IncludeHelp, on September 07, 2018

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

There are two methods to add string (text) at the end of the string:

  1. String.append() Method
  2. By using += operator

String.append() Method: Appends text at the end of the string. Read more: String.append() Method with Example

Another way is to add text by using +=, this operator concatenates the text in the given 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'

Program:

# Python program to add strings 
# to the string 

str = 'New Delhi'
str += ' '    #adding space 
str += 'Chennai'
str += ' '    #adding space
str += 'Mumbai'
str += ' '    #adding space 
str += 'Banglore'

# print the string 
print 'str:',str

Output

str: New Delhi Chennai Mumbai Banglore





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.