python中字符串拼接_python中字符串的常用拼接

本文介绍了Python中三种常见的字符串拼接方式:使用加号+,百分号%,以及join方法。通过实例展示了如何将字符串连接起来,包括字符串与变量的组合,以及列表元素的拼接。了解这些方法对于Python字符串操作至关重要。
摘要由CSDN通过智能技术生成

1、 + 号拼接

>>> test1 = "abcd"

>>> test2 = "opqr"

>>> test3 = "xyzh"

>>> test1 + test2

'abcdopqr'

>>> test2 + test3

'opqrxyzh'

>>> test2 + test1 + test3

'opqrabcdxyzh'

2、 % 号拼接

>>> test1 = "the price of apple:"

>>> test2 = 20.55666

>>> "%s is %.1f !" % (test1,test2)

'the price of apple: is 20.6 !'

>>> test3 = "mmmm"

>>> test4 = "nnnn"

>>> "test3 + test4 is %s+%s" % (test3,test4)

'test3 + test4 is mmmm+nnnn'

3、join拼接

>>> test1 = ["aaa","ccc","xxx","mmm"]

>>> "_".join(test1)

'aaa_ccc_xxx_mmm'

>>> test1 = ("aaa","ccc","xxx","mmm")

>>> "_".join(test1)

'aaa_ccc_xxx_mmm'

标签:test1,test3,test2,aaa,test4,python,xxx,拼接,字符串

来源: https://www.cnblogs.com/liujiaxin2018/p/14192097.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值