- 用法:- str.split(sep=None, maxsplit=- 1)
- 返回字符串中單詞的列表,使用 - sep作為分隔符字符串。如果給定- maxsplit,則最多完成- maxsplit拆分(因此,列表最多包含- maxsplit+1元素)。如果未指定- maxsplit或- -1,則拆分數量沒有限製(進行所有可能的拆分)。- 如果給出 - sep,則連續的分隔符不會組合在一起,並被視為分隔空字符串(例如,- '1,,2'.split(',')返回- ['1', '', '2'])。- sep參數可能由多個字符組成(例如,- '1<>2<>3'.split('<>')返回- ['1', '2', '3'])。使用指定的分隔符拆分空字符串會返回- ['']。- 例如: - >>> '1,2,3'.split(',') ['1', '2', '3'] >>> '1,2,3'.split(',', maxsplit=1) ['1', '2,3'] >>> '1,2,,3,'.split(',') ['1', '2', '', '3', '']- 如果 - sep未指定或為- None,則應用不同的拆分算法:連續空格的運行被視為單個分隔符,如果字符串有前導或,則結果將在開頭或結尾不包含空字符串尾隨空格。因此,使用- None分隔符拆分空字符串或僅包含空格的字符串將返回- []。- 例如: - >>> '1 2 3'.split() ['1', '2', '3'] >>> '1 2 3'.split(maxsplit=1) ['1', '2 3'] >>> ' 1 2 3 '.split() ['1', '2', '3']
相關用法
- Python str.splitlines用法及代碼示例
- Python str.strip用法及代碼示例
- Python str.isidentifier用法及代碼示例
- Python str.expandtabs用法及代碼示例
- Python str.isupper用法及代碼示例
- Python str.title用法及代碼示例
- Python str.rstrip用法及代碼示例
- Python str.lstrip用法及代碼示例
- Python str.removeprefix用法及代碼示例
- Python str.zfill用法及代碼示例
- Python str.format_map用法及代碼示例
- Python str.removesuffix用法及代碼示例
- Python str() vs repr()用法及代碼示例
- Python string轉integer用法及代碼示例
- Python string strip()用法及代碼示例
- Python string.octdigits用法及代碼示例
- Python string.whitespace用法及代碼示例
- Python strip()用法及代碼示例
- Python str()用法及代碼示例
- Python string capitalize()用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 str.split。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
