将列表中的字符拼接成字符串时,有两种方式
方式1:使用join()方法,将列表转为字符串
方式2:使用+运算符,循环遍历
1 import time 2 str1 = ['a','b','c','d','e','f','g']*100000 3 4 start_time = time.time() 5 output1 = '' 6 for v in str1: 7 output1 += v 8 stop_time = time.time() 9 print(stop_time-start_time) 10 11 start_time = time.time() 12 output2 = ''.join(str1) 13 stop_time = time.time() 14 print(stop_time-start_time)
0.1740097999572754 0.006000518798828125
可以看到: