python字符串拆分 split()方法

python字符串拆分 split()函数
str = "I love you"
str.split(' ')
#Output: ['I', 'love', 'you']

忽略sep参数或sep参数为None时,split()会先去除字符串两端的空白符,然后以任意长度的空白字符串作为界定符分切字符串(即连续的空白符串被当作单一的空白符对待)

str = " hello   world!"
str.split()
#Output: ['hello', 'world!']

对于有sep参数的,则认为两个连续的sep之间存在一个空字符串。

str = " hello   world!"
str.split(' ')
#Output: ['', 'hello', '', '', 'world!']
发布了54 篇原创文章 · 获赞 17 · 访问量 9172

猜你喜欢

转载自blog.csdn.net/qq_41979513/article/details/103108824