本文摘自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如何整段注释
Python中enumerate什么意思
Python中有哪些基本数据类型
Python工程师需要会什么
Python怎么读写文件
Python 2 map() reduce()函数用法讲解
Python如何获取列表长度?(代码示例)
Python3中时间处理与定时任务的方法介绍(附代码)
为什么要设计好目录结构?
Python变量类型-字典的实战运用与分析
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 总结3种Python合并字符串方法