python中字符串链接的七种方式


一. str1+str2

string类型 ‘+’号连接

>>> str1="one" >>> str2="two" >>> str1+str2 'onetwo' >>>
注意:该方式性能较差,因为python中字符串是不可变的类型,使用 + 连接两个字符串时会生成一个新的字符串,生成新的字符串就需要重新申请内存,当连续相加的字符串很多时(a+b+c+d+e+f+...) ,效率低下就是必然的了
例如:

#!/usr/bin/evn python
# -*- coding: utf-8 -*-
#Author: Johnson Chen

from time import time

def lz():
    t = time()
    for i in range(10000):
        s = s = '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州' + '兰州'
    print(time() -t)
def v5():
    t = time()
    for i in range(10000):
        s = ''.join(['威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武','威武'])
    print(time() -t)
lz() is v5()
View Code

运行结果为:

0.19512629508972168

0.06001925468444824

前后两种方式的性能有3倍的差距。

二. str1,str2

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

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

三. “%s%s” %(str1,str2)

string类型占位符连接

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

四. str1 str2

string类型空格自动连接

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

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

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

五. 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' >>>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM