如何连接字符串而不必多次输入?

时间:2015-02-11 22:23:19

标签: python string-concatenation

有没有办法写它所以我不必输入+ str(random.choice(string.ascii_uppercase))3次?

strip 1 ''.join([c for c in s if c not in ('!',' ',',',)])
strip2 = strip1 + str(random.choice(string.ascii_uppercase))+ str(random.choice(string.ascii_uppercase))+ str(random.choice(string.ascii_uppercase))
return (strip2)

2 个答案:

答案 0 :(得分:1)

使用for loop

strip2 = strip1 

for i in range(3):

    strip2 += str(random.choice(string.ascii_uppercase))

答案 1 :(得分:0)

list comprehension + range:

strip2 = strip1 + ''join([str(random.choice(string.ascii_uppercase)) for x in range(3)])