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中字符串和数字拼接报错的方法
Oct 23 Python
Python IDLE 错误:IDLE''s subprocess didn''t make connection 的解决方案
Feb 13 Python
python生成式的send()方法(详解)
May 08 Python
通过Pandas读取大文件的实例
Jun 07 Python
浅谈python实现Google翻译PDF,解决换行的问题
Nov 28 Python
python中字符串数组逆序排列方法总结
Jun 23 Python
python实现beta分布概率密度函数的方法
Jul 08 Python
使用Python实现 学生学籍管理系统
Nov 26 Python
python利用JMeter测试Tornado的多线程
Jan 12 Python
python_matplotlib改变横坐标和纵坐标上的刻度(ticks)方式
May 16 Python
Python实现加密接口测试方法步骤详解
Jun 05 Python
Python访问Redis的详细操作
Jun 26 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
ThinkPHP使用PHPExcel实现Excel数据导入导出完整实例
2014/07/22 PHP
PHP滚动日志的代码实现
2015/06/10 PHP
PHP如何通过AJAX方式实现登录功能
2015/11/23 PHP
PHP简单实现解析xml为数组的方法
2018/05/02 PHP
利用cookie记住背景颜色示例代码
2013/11/04 Javascript
js取值中form.all和不加all的区别介绍
2014/01/20 Javascript
网页运行时提示对象不支持abigimage属性或方法
2014/08/10 Javascript
Javascript显示和隐藏ul列表的方法
2015/07/15 Javascript
阿里巴巴技术文章分享 Javascript继承机制的实现
2016/01/14 Javascript
JavaScript模拟数组合并concat
2016/03/06 Javascript
JavaScript计算器网页版实现代码分享
2016/07/15 Javascript
深入探讨Vue.js组件和组件通信
2016/09/12 Javascript
深入理解jquery中的each用法
2016/12/14 Javascript
基于js的变量提升和函数提升(详解)
2017/09/17 Javascript
vue实现父子组件之间的通信以及兄弟组件的通信功能示例
2019/01/29 Javascript
JS查找孩子节点简单示例
2019/07/25 Javascript
nodejs nedb 封装库与使用方法示例
2020/02/06 NodeJs
对python中raw_input()和input()的用法详解
2018/04/22 Python
windows下添加Python环境变量的方法汇总
2018/05/14 Python
pyqt弹出新对话框,以及关闭对话框获取数据的实例
2019/06/18 Python
Python递归函数特点及原理解析
2020/03/04 Python
解决python多线程报错:AttributeError: Can't pickle local object问题
2020/04/08 Python
 Alo Yoga官网:购买瑜伽服装
2018/06/17 全球购物
servlet面试题
2012/08/20 面试题
金融专业个人求职信
2013/09/22 职场文书
大学毕业登记表自我鉴定
2013/10/09 职场文书
财务会计专业推荐信
2013/11/30 职场文书
总账会计岗位职责
2014/03/13 职场文书
影子教师研修方案
2014/06/14 职场文书
先进个人评语大全
2015/01/04 职场文书
践行三严三实心得体会(2016推荐篇)
2016/01/06 职场文书
CSS3 菱形拼图实现只旋转div 背景图片不旋转功能
2021/03/30 HTML / CSS
Django使用redis配置缓存的方法
2021/06/01 Redis
mybatis 解决从列名到属性名的自动映射失败问题
2021/06/30 Java/Android
浅谈redis整数集为什么不能降级
2021/07/25 Redis
mysql 获取相邻数据项
2022/05/11 MySQL