- 用法:- 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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
