python字符串拼接

python字符串拼接Python字符串拼接在Python的实际开发中,很多都需要用到字符串拼接,python中字符串拼接有很多,今天总结一下:用+符号拼接用%符号拼接用join()方法拼接用format()方法

大家好,又见面了,我是你们的朋友全栈君。

Python字符串拼接

Python的实际开发中,很多都需要用到字符串拼接,python中字符串拼接有很多,今天总结一下:

  • +符号拼接
  • %符号拼接
  • join()方法拼接
  • format()方法拼接
  • string模块中的Template对象

如果还有其他方法,欢迎补充。 
例子:

fruit1 = 'apples'
fruit2 = 'bananas'
fruit3 = 'pears'

 

要求: 
输出字符串’There are apples, bananas, pears on the table’

1. 用+符号拼接

+拼接字符串如下: 

1 str = 'There are'+fruit1+','+fruit2+','+fruit3+' on the table' 

该方法效率比较低,不建议使用

2. 用%符号拼接

%符号拼接方法如下: 

1 str = 'There are %s, %s, %s on the table.' % (fruit1,fruit2,fruit3) 

除了用元组的方法,还可以使用字典如下: 

1 str = 'There are %(fruit1)s,%(fruit2)s,%(fruit3)s on the table' % {'fruit1':fruit1,'fruit2':fruit2,'fruit3':fruit3} 

该方法比较通用

3. 用join()方法拼接

join()`方法拼接如下

1 temp = ['There are ',fruit1,',',fruit2,',',fruit3,' on the table']
2 ''.join(temp)

该方法使用与序列操作

4. 用format()方法拼接

format()方法拼接如下:

4. 用format()方法拼接

format()方法拼接如下:

1 str = 'There are {}, {}, {} on the table'
2 str.format(fruit1,fruit2,fruit3)

 

还可以指定参数对应位置:

1 str = 'There are {2}, {1}, {0} on the table'
2 str.format(fruit1,fruit2,fruit3) #fruit1出现在0的位置

 

同样,也可以使用字典:

1 str = 'There are {fruit1}, {fruit2}, {fruit3} on the table'
2 str.format(fruit1=fruit1,fruit2=fruit2,fruit3=fruit3)

5. 用string模块中的Template对象

string模块中的Template对象如下:

1 from string import Template
2 str = Template('There are ${fruit1}, ${fruit2}, ${fruit3} on the table') #此处用的是{},别搞错了哦
3 str.substitute(fruit1=fruit1,fruit2=fruit2,fruit3=fruit3) #如果缺少参数,或报错如果使用safe_substitute()方法不会
4 str.safe_substitute(fruit1=fruit1,fruit2=fruit2) 
5 #输出'There are apples, bananas, ${fruit3} on the table'

 

总结

拼接的方法有多种,不同场合下使用不同的方法,个人比较推荐%format()方法,简单方便。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/155949.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • fatal: unable to access https:// Failed to connect to: Connection refused|git clone问题(完美解决)

    fatal: unable to access https:// Failed to connect to: Connection refused|git clone问题(完美解决)fatal:unabletoaccess‘https://github.com/xxxx/’:Failedtoconnecttox.x.x.xportxxxxx:Connectionrefused|gitclone问题(完美解决)系统:ubuntu14.04问题描述执行以下命令克隆目标源码到本地时,会出现错误。gitclonehttps://gith…

    2022年6月21日
    59
  • pledge to_debezium采集 oracle

    pledge to_debezium采集 oracle下面我们来看两个范例,一个是使用BULKCOLLECT的,一个没有:范例1、使用多次循环来检索并显示数据。范例2、一次性获取数据,然后再循环显示。下面是一些有关BULKCOLLECT的建议:1、从Oracle9i开始,你都可以在静态SQL和动态SQL中使用BULKCOLLECT。2、你可以在任何地方的SELECTINTO,FETCHINTO,和RETURNINGINTO子句中使用BUL…

    2025年5月25日
    0
  • oracle分页查询解释

    oracle分页查询解释select*fromt_userorderbyuser_id;——————————————–分页的必须参数–当前页–每页几条数据–一共多少页–总记录数————对于分页查询而言,最终需要两个参数(一个是开始条数,一个是结束条数)———select*from(SELEC…

    2022年5月28日
    37
  • python 比较字符串是否一样

    python 比较字符串是否一样在python中,判断两个变量是否相等或一样,可以使用==或者is来判断;判断不一样可以使用isnot。示例1.有时候两个字符串打印出来看着一样,但是判断却是False?如果两个字符串末尾有其他符号,比如回车‘\n’,print的时候无法发现的,所以需要strip:a=a.strip()b=b.strip()ifa==b: print"True"2.有时候==判断是Tr…

    2022年6月18日
    26
  • pycharm有什么好用的插件_pycharm插件推荐

    pycharm有什么好用的插件_pycharm插件推荐目录一、安装二、导入及设置三、使用一、安装在全局环境中(不要在虚拟环境中安装pipinstallautopep8二、导入及设置在PyCharm导入这个工具,具体设置如下图:Name:AutoPep8Description:autopep8yourcodeProgram:autopep8Arguments:–in-place–aggressive–aggressive$FilePath$Workingdirectory:$ProjectFileDir$

    2022年8月29日
    1
  • 股票布林线boll指标介绍及应用(图解)_布林线和什么指标配合效果好

    股票布林线boll指标介绍及应用(图解)_布林线和什么指标配合效果好一、定义:布林线指标,即BOLL指标,其英文全称是“BollingerBands”,布林线(BOLL)由约翰·布林先生创造,其利用统计原理,求出股价的标准差及其信赖区间,从而确定股价的波动

    2022年8月2日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号