Python3 string.split()方法

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

语法

以下是 split()方法的语法 -
str.split(str="", num=string.count(str)).

参数

  • str -- 这是分隔符,默认情况下使用空格

  • num -- 这是指定要作出的行数

返回值

此方法返回行的列表。

示例

下面的示例显示 split()方法的使用。
#!/usr/bin/python3
str = "this is string example....wow!!!"
print (str.split( ))
print (str.split('i',1))
print (str.split('w'))

当我们运行上面的程序,会产生以下结果 -
['this', 'is', 'string', 'example....wow!!!']
['th', 's is string example....wow!!!']
['this is string example....', 'o', '!!!']

上一篇: Python3数字 下一篇: Python3字符串