Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

split在python中的用法

作者:—上花′Ing   发布日期:2025-09-14   浏览:36

# 示例代码:split在Python中的用法

# split() 方法通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串
# 参数:
# - sep: 分隔符。默认为空格或换行符等空白字符。可以指定分隔符,如逗号、分号等。
# - maxsplit: 分割次数。默认为 -1, 即不分割次数。

text = "hello, world! welcome to python"

# 使用空格作为分隔符,默认分割所有
result1 = text.split()
print(result1)  # 输出: ['hello,', 'world!', 'welcome', 'to', 'python']

# 使用逗号作为分隔符
result2 = text.split(',')
print(result2)  # 输出: ['hello', ' world! welcome to python']

# 使用空格作为分隔符,并指定最大分割次数为1
result3 = text.split(' ', 1)
print(result3)  # 输出: ['hello,', 'world! welcome to python']

# 使用多个字符作为分隔符
import re
text_with_multiple_separators = "hello,   world!  welcome;to:python"
result4 = re.split(r'[,\s;:]+', text_with_multiple_separators)
print(result4)  # 输出: ['hello', 'world!', 'welcome', 'to', 'python']

上一篇:python sleep

下一篇:python map

大家都在看

python时间格式

python读取文件路径

staticmethod在python中有

python开发windows应用程序

python中len是什么意思

python ord和chr

python中的yield

python自定义异常

python判断路径是否存在

python list.pop

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站