文章目录
- 当不指定 sep 参数时,split() 会使用任何空白字符作为分隔符,并自动忽略多余的空格。 text = “Hello World! How are you?” words = text.split() print(words) # [‘Hello’, ‘World!’, ‘How’, ‘are’, ‘you?’]
- text = “apple,banana,cherry,dates” fruits = text.split(‘,’) print(fruits) # [‘apple’, ‘banana’, ‘cherry’, ‘dates’]
- import re text = “one–two—three—-four” parts = re.split(‘–+’, text) print(parts) # [‘one’, ‘two’, ‘three’, ‘four’]
- text = “one two three four five” parts = text.split(‘ ‘, 2) print(parts) # [‘one’, ‘two’, ‘three four five’]
- # 分割空字符串,不指定分隔符 print(“”.split()) # 输出: [] # 分割空字符串,指定分隔符 print(“”.split(‘,’)) # 输出: [”] # 分割只有一个字符的字符串 print(“a”.split(‘,’)) # 输出: [‘a’]
- text = “one,,two,three,,four” parts = text.split(‘,’) print(parts) # [‘one’, ”, ‘two’, ‘three’, ”, ‘four’] 如果需要忽略连续的分隔符,可以使用正则表达式: import re text = “one,,two,three,,four” parts = re.split(r’,+’, text) print(parts) # [‘one’, ‘two’, ‘three’, ‘four’]
- rsplit() 方法与 split() 类似,但从字符串的右侧开始分割。 text = “one two three four” # 从左侧分割 print(text.split(‘ ‘, 2)) # 输出: [‘one’, ‘two’, ‘three four’] # 从右侧分割 print(text.rsplit(‘ ‘, 2)) # 输出: [‘one two’, ‘three’, ‘four’]
- 使用正则表达式模块 re 可以实现复杂的分隔符分割。 import re text = “apple;banana, cherry|dates” parts = re.split(r'[;,|s]+’, text) print(parts) # [‘apple’, ‘banana’, ‘cherry’, ‘dates’]
- splitlines() 方法用于按照行边界拆分多行字符串,而 split('') 也可以实现类似功能,但 splitlines() 更加全面,能够处理不同平台的换行符。 text = “HellonWorldr PythonrAnother line” # 使用 split(‘ ‘) print(text.split(‘ ‘)) # 输出: [‘Hello’, ‘Worldr’, ‘PythonrAnother line’] # 使用 splitlines() print(text.splitlines()) # 输出: [‘Hello’, ‘World’, ‘Python’, ‘Another line’]
- 如果分隔符出现在字符串的开头或结尾,split() 会在结果中包含空字符串。 text = “,apple,banana,,cherry,” parts = text.split(‘,’) print(parts) # 输出: [”, ‘apple’, ‘banana’, ”, ‘cherry’, ”] 如果不需要开头和结尾的空字符串,可以结合 strip() 方法使用: parts = text.strip(‘,’).split(‘,’) print(parts) # 输出: [‘apple’, ‘banana’, ”, ‘cherry’]
- split() 方法同样适用于包含 Unicode 字符的字符串。 text = “你好,世界,Python” parts = text.split(‘,’) print(parts) # 输出: [‘你好’, ‘世界’, ‘Python’]
目录
- 一、基本语法
- 1. sep(可选)
- 2. maxsplit(可选)
- 二、常见用法示例
- 1. 使用默认分隔符(空白)
- 2. 指定分隔符
- 3. 使用多个字符作为分隔符(结合正则使用)
- 4. 指定最大分割次数
- 5. 分割空字符串
- 6. 处理连续的分隔符
- 三、高级用法
- 1. rsplit()
- 2. 分割包含多种分隔符的字符串
- 4. 分割多行字符串
- 5. 分隔符在字符串的开头或结尾
- 6. 处理Unicode字符
- 总结
split()是 Python 中字符串(str)对象的一个非常强大的内置方法,用于将字符串按照指定的分隔符拆分成多个子字符串,并返回一个包含这些子字符串的列表。
str.split(sep = None, maxsplit = -1)
text = "a-b-c-d-e-f"
parts = text.split('-', 3)
print(parts) # 输出: ['a', 'b', 'c', 'd-e-f']
指定用于分割字符串的分隔符。如果未指定,默认使用任何空白字符(如空格、制表符 t、换行符 等)作为分隔符
指定分割的最大次数。如果提供了该参数,字符串将被分割成 maxsplit + 1 个子字符串。默认值为 -1,表示不限制分割次数,即尽可能多地分割。
当不指定 sep 参数时,split() 会使用任何空白字符作为分隔符,并自动忽略多余的空格。
text = "Hello World! How are you?" words = text.split() print(words) # ['Hello', 'World!', 'How', 'are', 'you?']
text = "apple,banana,cherry,dates"
fruits = text.split(',')
print(fruits) # ['apple', 'banana', 'cherry', 'dates']
import re
text = "one--two---three----four"
parts = re.split('--+', text)
print(parts) # ['one', 'two', 'three', 'four']
text = "one two three four five"
parts = text.split(' ', 2)
print(parts) # ['one', 'two', 'three four five']
# 分割空字符串,不指定分隔符
print("".split()) # 输出: []
# 分割空字符串,指定分隔符
print("".split(',')) # 输出: ['']
# 分割只有一个字符的字符串
print("a".split(',')) # 输出: ['a']
text = "one,,two,three,,four"
parts = text.split(',')
print(parts) # ['one', '', 'two', 'three', '', 'four']
如果需要忽略连续的分隔符,可以使用正则表达式:
import re text = "one,,two,three,,four" parts = re.split(r',+', text) print(parts) # ['one', 'two', 'three', 'four']
rsplit() 方法与 split() 类似,但从字符串的右侧开始分割。
text = "one two three four"
# 从左侧分割
print(text.split(' ', 2)) # 输出: ['one', 'two', 'three four']
# 从右侧分割
print(text.rsplit(' ', 2)) # 输出: ['one two', 'three', 'four']
使用正则表达式模块 re 可以实现复杂的分隔符分割。
import re text = "apple;banana, cherry|dates" parts = re.split(r'[;,|s]+', text) print(parts) # ['apple', 'banana', 'cherry', 'dates']
splitlines() 方法用于按照行边界拆分多行字符串,而 split('') 也可以实现类似功能,但 splitlines() 更加全面,能够处理不同平台的换行符。
text = "HellonWorldr
PythonrAnother line"
# 使用 split('
')
print(text.split('
')) # 输出: ['Hello', 'Worldr', 'PythonrAnother line']
# 使用 splitlines()
print(text.splitlines()) # 输出: ['Hello', 'World', 'Python', 'Another line']
如果分隔符出现在字符串的开头或结尾,split() 会在结果中包含空字符串。
text = ",apple,banana,,cherry,"
parts = text.split(',')
print(parts) # 输出: ['', 'apple', 'banana', '', 'cherry', '']
如果不需要开头和结尾的空字符串,可以结合 strip() 方法使用:
parts = text.strip(',').split(',')
print(parts) # 输出: ['apple', 'banana', '', 'cherry']
split() 方法同样适用于包含 Unicode 字符的字符串。
text = "你好,世界,Python"
parts = text.split(',')
print(parts) # 输出: ['你好', '世界', 'Python']
到此这篇关于Python中split()方法常见用法总结的文章就介绍到这了,更多相关Python split()方法用法内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!
您可能感兴趣的文章:
- 举例详解Python中的split()函数的使用方法
- 在Python中用split()方法分割字符串的使用介绍
- python实现字符串完美拆分split()的方法
- Python split() 函数拆分字符串将字符串转化为列的方法
- Python-split()函数实例用法讲解
- Python split()函数使用方法详解
- python中split()函数的用法详解