本文摘自php中文网,作者伊谢尔伦,侵删。
这篇文章主要介绍了Python合并字符串的3种方法,本文讲解了使用+=操作符、使用%操作符、使用String的' '.join()方法3种方法,需要的朋友可以参考下目的
将一些小的字符串合并成一个大字符串,更多考虑的是性能
方法
常见的方法有以下几种:
1.使用+=操作符
1 | BigString = small1 + small2 + small3 + ... + smalln
|
例如有一个片段pieces=['Today','is','really','a','good','day'],我们希望把它联起来
1 2 3 | BigString = ' '
for e in pieces:
BigString + = e + ' '
|
或者用
1 2 | import operator
BigString = reduce (operator.add,pieces, ' ' )
|
2.使用%操作符
1 2 | In [ 33 ]: print '%s,Your current money is %.1f' % ( 'Nupta' , 500.52 )
Nupta,Your current money is 500.5
|
3.使用String的' '.join()方法
1 2 | In [ 34 ]: ' ' .join(pieces)
Out[ 34 ]: 'Today is really a good day'
|
关于性能
阅读剩余部分
相关阅读 >>
Python中sep是函数吗?该怎么使用?
Python实现合并同一个文件夹下所有txt文件的方法
Python 删除列表里所有空格项的方法
Python中的【//】是什么运算符号
Python可以用来干什么?
hash()是Python内置的吗
Python利用不到一百行代码实现一个小siri
方法示例Python如何把字典写入到csv文件的
Python中的元类(metaclass)是什么
Python中的输入与输出是什么?(实例详解)
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 总结3种Python合并字符串方法