Pythonic创建字符串+ vs%的方法

时间:2013-08-29 19:10:22

标签: python string append

当从许多子串创建一个字符串时,更多的是pythonic - +或%?

big_string = string1 + string2 + ... + stringN

big_string = ''
for i in range(n):
    big_string+=str(i)

big_string = "%s%s...%s" % (string1, string2, ... , stringN)

big_string = ''
for i in range(n):
    big_string = "%s%s" % (big_string, str(i))

2 个答案:

答案 0 :(得分:11)

big_string = ''.join([string1, string2, ..., stringN])

答案 1 :(得分:-2)

big_string = reduce(lambda x, y: x + y, [string1, string2, ..., stringN], "")