Python实现string字符串连接的方法总结【8种方式】


Posted in Python onJuly 06, 2018

本文实例总结了Python实现string字符串连接的方法。分享给大家供大家参考,具体如下:

以下基于python 2.7版本,代码片段真实有效。

一. str1+str2

string类型 ‘+'号连接

>>> str1="one"
>>> str2="two"
>>> str1+str2
'onetwo'
>>>

二. str1,str2

string类型 ‘,'号连接成tuple类型

>>> str1="one"
>>> str2="two"
>>> str1 ,str2
('one', 'two')
>>> type((str1 ,str2))
<type 'tuple'>
>>>

三. 格式化字符串连接

string类型格式化连接

1.常见的格式化方式

>>> str1="one"
>>> str2="two"
>>> "%s%s"%(str1,str2)
'onetwo'

2.高级点的format 格式化

>>> "{test}_666@{data:.2f}".format(test="Land", data=10.1)
'Land_666@10.10'

3.鲜为人知的【%(word)typeprint函数格式化

>>> print "%(test)s666%(last)d" % {"test": "Land", "last": 101}
Land666101

四. str1 str2

string类型空格自动连接

>>> "one" "two"
'onetwo'

这里需要注意的是,参数不能代替具体的字符串写成
错误方式:

>>> str1="one"
>>> str2="two"
>>> str1 str2
 File "<stdin>", line 1
  str1 str2
      ^
SyntaxError: invalid syntax

五. str1 \ str2 \str3

string类型反斜线多行连接

>>> test = "str1 " \
... "str2 " \
... "str3"
>>> test
'str1 str2 str3'
>>>

六. M*str1*N

string类型乘法连接

>>> str1="one"
>>> 1*str1*4
'oneoneoneone'
>>>

七. join方式连接

string类型join方式连接list/tuple类型

>>> str1="one"
>>> list1=["a","b","c"]
>>> tuple1=("H","I","J")
>>> str1.join(list1)
'aonebonec'
>>> str1.join(tuple1)
'HoneIoneJ'

这里的join有点像split的反操作,将列表或元组用指定的字符串相连接;

但是值得注意的是,连接的列表或元组中元素的类型必须全部为string类型,否则就可能报如下的错误:

>>> list2=["a",2,"c",4.3]
>>> str1.join(list2)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: sequence item 1: expected string, int found
>>>

join还有一个妙用,就是将所有list或tuple中的元素连接成string类型并输出;

>>> list1
['a', 'b', 'c']
>>> "".join(list1)
'abc'
>>> type("".join(list1))
<type 'str'>
>>>

八.列表推导方式连接

与join方式类似

>>> "".join(["Land" for i in xrange(3)])
'LandLandLand'
>>> "0".join(["Land" for i in xrange(2)])
'Land0Land'
>>>

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python设计模式之装饰模式实例详解
Jan 21 Python
pymongo中group by的操作方法教程
Mar 22 Python
Django框架自定义session处理操作示例
May 27 Python
PyQt QListWidget修改列表项item的行高方法
Jun 20 Python
python opencv minAreaRect 生成最小外接矩形的方法
Jul 01 Python
详解Python中正则匹配TAB及空格的小技巧
Jul 26 Python
Django中使用session保持用户登陆连接的例子
Aug 06 Python
Python 写入训练日志文件并控制台输出解析
Aug 13 Python
python  logging日志打印过程解析
Oct 22 Python
Python性能测试工具Locust安装及使用
Dec 01 Python
MoviePy简介及Python视频剪辑自动化
Dec 18 Python
python处理json数据文件
Apr 11 Python
python 读取目录下csv文件并绘制曲线v111的方法
Jul 06 #Python
Python 爬虫之Beautiful Soup模块使用指南
Jul 05 #Python
Python实现模拟登录网易邮箱的方法示例
Jul 05 #Python
python selenium自动上传有赞单号的操作方法
Jul 05 #Python
python实现爬取图书封面
Jul 05 #Python
Python定义二叉树及4种遍历方法实例详解
Jul 05 #Python
Python使用pyodbc访问数据库操作方法详解
Jul 05 #Python
You might like
使用php检测用户当前使用的浏览器是否为IE浏览器
2013/12/03 PHP
PHP中把错误日志保存在系统日志中(Windows系统)
2015/06/23 PHP
Yii2实现ajax上传图片插件用法
2016/04/28 PHP
smarty中改进truncate使其支持中文的方法
2016/05/30 PHP
javascript网页关闭时提醒效果脚本
2008/10/22 Javascript
jQuery判断元素是否存在的可靠方法
2014/05/06 Javascript
Node.js的环境安装配置(使用nvm方式)
2016/10/11 Javascript
js实现贪吃蛇小游戏(容易理解)
2017/01/22 Javascript
浅谈vue自定义全局组件并通过全局方法 Vue.use() 使用该组件
2017/12/07 Javascript
vue awesome swiper异步加载数据出现的bug问题
2018/07/03 Javascript
Redux实现组合计数器的示例代码
2018/07/04 Javascript
element ui table 增加筛选的方法示例
2018/11/02 Javascript
Javascript实现秒表计时游戏
2020/05/27 Javascript
nodejs+koa2 实现模仿springMVC框架
2020/10/21 NodeJs
VUE+Element实现增删改查的示例源码
2020/11/23 Vue.js
[05:31]DOTA2英雄梦之声_第04期_光之守卫
2014/06/23 DOTA
[01:08:33]OG vs VGJ.T 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
[05:09]DOTA2-DPC中国联赛2月22日Recap集锦
2021/03/11 DOTA
Python删除指定目录下过期文件的2个脚本分享
2014/04/10 Python
简单的Apache+FastCGI+Django配置指南
2015/07/22 Python
Python批量修改文本文件内容的方法
2016/04/29 Python
Python实现类的创建与使用方法示例
2017/07/25 Python
python中使用psutil查看内存占用的情况
2018/06/11 Python
Python命名空间的本质和加载顺序
2018/12/17 Python
Python的Lambda函数用法详解
2019/09/03 Python
pytorch 使用加载训练好的模型做inference
2020/02/20 Python
canvas简易绘图的实现(海绵宝宝篇)
2018/07/04 HTML / CSS
阿根廷旅游网站:almundo阿根廷
2018/02/12 全球购物
普通大学毕业生自荐信
2013/11/04 职场文书
一年级学生评语
2014/04/23 职场文书
贷款委托书怎么写
2014/08/02 职场文书
教师批评与自我批评剖析材料
2014/10/16 职场文书
2015教师年度工作总结范文
2015/04/07 职场文书
员工旷工检讨书
2015/08/15 职场文书
Django路由层如何获取正确的url
2021/07/15 Python
HTML基础详解(下)
2021/10/16 HTML / CSS