Python05-09_字符串的分割与合并

2021-03-29 00:27

阅读:473

标签:range   app   +=   分隔符   print   pytho   load   append   div   

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次

技术图片

 

Python05-09_字符串的分割与合并

标签:range   app   +=   分隔符   print   pytho   load   append   div   

原文地址:https://www.cnblogs.com/xujie-0528/p/13620228.html


评论


亲,登录后才可以留言!