python split函数

1,split函数不能指定空的字符串为定界符,如下面的例子:

s = 'abc'
s.split('')
#报错
ValueError                                Traceback (most recent call last)
<ipython-input-15-a0f05887ecdc> in <module>
      1 s = 'abc'
----> 2 s.split('')

ValueError: empty separator

2,可以不提供任何定界符,这时函数会以任意分隔符为定界符。

s = 'ab\nc\tcd e  g'
s.split()
#output
#['ab', 'c', 'cd', 'e', 'g']

3,将str转化为list怎么办?

直接用list函数:

s = 'abc'
list(s)
#output
#s = 'abc'
list(s)

猜你喜欢

转载自blog.csdn.net/linkequa/article/details/88531526