Python | 使用+ =运算符在字符串末尾附加文本

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

给定一个字符串,我们必须在Python中使用+ =运算符在字符串的末尾附加更多字符串(文本)。

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

有两种方法可以在字符串末尾添加字符串(文本):

  1. String.append() Method

    String.append()方法

  2. By using += operator

    通过使用+ =运算符

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

String.append()方法:在字符串末尾附加文本。 阅读更多: 带示例的String.append()方法

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


翻译自: https://www.includehelp.com/python/appending-text-at-the-end-of-the-string-using-compound-assignment.aspx

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值