Python str.split方法

来源:互联网 发布:淘宝上专柜代购的衣服 编辑:程序博客网 时间:2024/06/05 19:36

str.split(sep = None)可用于分割字符串

不传入sep参数时

以str里的所有空字符为分割符将str分割,特点是:
* 空字符包括: 空格,\n, \r\n, \t, \r
* 连续多个空字符被当作一个分割符
* 字符串开头和末尾处的空字符会被trim掉
返回一个不包含空字符串的字符串list

print ('\t              a b\r c\r\n   '.split())
['a', 'b', 'c']

传入sep参数时

print('a,b,,c'.split(','))
['a', 'b', '', 'c']
print(', a,b,c'.split(','))
['', ' a', 'b', 'c']

可以看出, 这时它不会有trim操作, 不会将连续的分割符当成一个来处理,会有空字符串存在

References

*https://docs.python.org/2/library/stdtypes.html?highlight=split#str.split

0 0
原创粉丝点击