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列表