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', '!!!']