1

当从许多子字符串中创建一个字符串时,哪个更 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))
4

2 回答 2

11
big_string = ''.join([string1, string2, ..., stringN])
于 2013-08-29T19:11:36.127 回答
-2

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

于 2013-08-29T19:23:09.300 回答