# 示例代码:按分隔符进行分割
# 使用字符串的split方法可以按照指定的分隔符将字符串分割成列表
text = "apple,banana,orange"
result = text.split(",") # 按逗号分割
print(result) # 输出: ['apple', 'banana', 'orange']
# 如果不指定分隔符,默认会按空格分割
text_with_spaces = "hello world this is python"
result_spaces = text_with_spaces.split() # 按空格分割
print(result_spaces) # 输出: ['hello', 'world', 'this', 'is', 'python']
# 可以指定最大分割次数
text_with_multiple_delimiters = "one,two,three,four"
result_maxsplit = text_with_multiple_delimiters.split(",", 2) # 最多分割两次
print(result_maxsplit) # 输出: ['one', 'two', 'three,four']
split() 方法:这是 Python 字符串对象的一个方法,用于根据指定的分隔符将字符串分割成一个列表。"," 表示按逗号分割。上一篇:python生成excel文件
下一篇:python调用exe程序
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站