开发者

Python中split()方法常见用法总结大全

开发者 https://www.devze.com 2025-05-18 09:22 出处:网络 作者: 码了又码
目录一、基本语法1. sep(可选)2. maxsplit(可选)二、常见用法示例1. 使用默认分隔符(空白)2. 指定分隔符3. 使用多个字符作为分隔符(结合正则使用)4. 指定最大分割次数5. 分割空字符串6. 处理连续的分隔符三、高级
目录
  • 一、基本语法
    • 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']

    1. sep(可选)

    指定用于分割字符串的分隔符。如果未指定,默认使用任何空白字符(如空格、制表符 \t、换行符 等)作为分隔符

    2. maxsplit(可选)

    指定分割的最大次数。如果提供了该参数,字符串将被分割成 maxsplit + 1 个子字符串。默认值为 -1,表示不限制分割次数,即尽可能多地分割。

    二、常见用法示例

    1. 使用默认分隔符(空白)

    当不指定 sep 参数时,split() 会使用任何空白字符作为分隔符,并自动忽略多余的空格。

    text = "Hello World!   How are you?"
    words = text.split()
    print(words) # ['Hello', 'World!', 'How', 'are', 'you?']

    2. 指定分隔符

    js
    text = "apple,bananjavascripta,cherry,dates"
    fruits = text.split(',')
    print(fruits) # ['apple', 'banana', 'cherry'编程, 'dates']

    3. 使用多个字符作为分隔符(结合正则使用)

    import re
    
    text = "one--two---three----four"
    parts = re.split('--+', text)
    print(parts) # ['one', 'two', 'three', 'four']

    4. 指定最大分割次数

    text = "one two three four five"
    parts = text.split(' ', 2)
    print(parts) # ['one', 'two', 'three four five']

    5. 分割空字符串

    # 分割空字符串,不指定分隔符
    print("".split())          # 输出: []
    
    # 分割空字符串,指定分隔符
    print("".split(','))       # 输出: ['']
    
    # 分割只有一个字符的字符串
    print("a".split(','))      # 输出: ['a']

    6. 处理连续的分隔符

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

    三、高级用法

    1. rsplit()

    rsplit() 方法与 split() 类似,但从字符串的右侧开始分割。

    text = "one two three four"
    
    # 从左侧分割
    print(text.split(' ', 2))   # 输出: ['one', 'two', 'three four']
    
    # 从右侧分割
    print(text.rsplit(' ', 2))  # 输出: ['one two', 'three', 'four']

    2. 分割包含多种分隔符的字符串

    使用正则表达式模块 re 可以实现复杂的分隔符分割。

    import re
    
    text = "apple;banana, cherry|dates"
    parts = re.split(r'[;,|\s]+', text)
    print(parts) # ['apple', 'banana', 'cherry', 'dates']

    4. 分割多行字符串

    splitlines() 方法用于按照行边界拆分多行字符串,而 split('') 也可以实现类似功能,但 splitlines() 更加全面,能够处理不同平台的换行符。

    text = "Hello\nWorld\r
    Python\rAnother line"
    
    # 使用 split('
    ')
    print(text.split('
    '))  # 输出: ['Hello', 'World\r', 'Python\rAnother line']
    
    # 使用 splitlines()
    print(text.splitlines())  # 输出: ['Hello', 'World', 'Python', 'Another line']

    5. 分隔符在字符串的开头或结尾

    如果分隔符出现在字符串的开头或结尾,split() 会在结果中包含空字符串。

    text = ",apple,banana,,cherry,"
    
    parts = text.split(',')
    print(parts)  # 输出: ['', 'apple', 'banana', '', 'cherry', '']

    如果不需要开头和结尾的空字符串,可以结合 strip() 方法使用:

    parts = text.strip(',').split(',')
    print(parts)  # 输出: ['apple', 'banana', '', 'cherry']

    6. 处理Unicode字符

    split() 方法同样适用于包含 Unicode 字符的字符串。

    text = "你好,世界,Python"
    partsandroid = text.split(',')
    print(phttp://www.devze.comarts)  # 输出: ['你好', '世界', 'Python']

    总结 

    到此这篇关于Python中split()方法常见用法总结的文章就介绍到这了,更多相关Python split()方法用法内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    关注公众号