spilt()分割和join()合并
- spilt()可以基于指定分割符将字符串分割成多个子字符串(存储到列表之中),如果不指定分隔符,则默认使用空白分隔符(换行符、空白,制表符)
1 a = "to be or not to be" 2 a = a.spilt() 3 print(a) 4 ['to','be','or','not','to','be']
- join()的作用刚好相反,用于将一系列的子字符串拼接起来
1 a = ['wo','ai','ni'] 2 a = '*'.join(a) 3 print(a) 4 'wo*ai*ni'
pratice: 比较加法和join两种拼接方式的效率
1 a = "" 2 import time 3 time01 = time.time() 4 for i in range(10000000): 5 a+= "sxt" 6 time02 = time.time() 7 print("时间:"+str(time02 - time01)) 8 9 time03 = time.time() 10 li = [] 11 for i in range(10000000): 12 li.append("sxt") 13 14 a="".join(li) 15 16 time04 = time.time() 17 print("时间:"+str(time04 - time03))
循环100w次和循环1000w次