Python字符串split()方法

Python字符串rstrip()方法返回字符串中所有单词的列表,使用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', '!!!']

上一篇: Python字符串 下一篇: Python列表