美文网首页python
python string split()

python string split()

作者: 庵下桃花仙 | 来源:发表于2018-05-09 22:59 被阅读15次

描述

方法split()返回字符串中所有单词的列表,使用str作为分隔符(如果未指定,则在所有空白处分割),可选地将分割数限制为num。

语法

str.split(str="", num=string.count(str)).

参量

str - 这是任何分隔符,默认情况下它是空格。
num - 分割的数量

返回值

此方法返回一个行列表。

例子

str = "Line1-abcdef \nLine2-abc \nLine4-abcd";
print str
print str.split( )
print str.split(' ', 1 )

结果

Line1-abcdef
Line2-abc
Line4-abcd
['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
['Line1-abcdef', '\nLine2-abc \nLine4-abcd']

相关文章

网友评论

    本文标题:python string split()

    本文链接:https://www.haomeiwen.com/subject/melarftx.html