python split()函数

split() 通过指定分隔符对字符串进行切片,如果第二个参数 num 有指定值,则分割为 num+1 个子字符串。

 

返回值: 返回分割后的字符串列表。

 

语法: str.split(分隔符, 分割次数) 

分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。

分割次数。默认为 -1, 即分隔所有。

str = "this is string example....wow!!!"
print (str.split( ))       # 以空格为分隔符
print (str.split('i',1))   # 以 i 为分隔符 分割2次
print (str.split('w'))     # 以 w 为分隔符

结果:

['this', 'is', 'string', 'example....wow!!!']
['th', 's is string example....wow!!!']
['this is string example....', 'o', '!!!']

 

posted @ 2022-06-06 10:52  zed99  阅读(284)  评论(0编辑  收藏  举报