# 示例代码:使用 split() 函数
# 基本用法:按照空格分割字符串
text = "Hello world this is Python"
result = text.split()
print(result) # 输出: ['Hello', 'world', 'this', 'is', 'Python']
# 指定分隔符:按照逗号分割字符串
text_with_commas = "apple,banana,orange"
result_with_commas = text_with_commas.split(',')
print(result_with_commas) # 输出: ['apple', 'banana', 'orange']
# 限制分割次数:maxsplit 参数
text_with_limit = "one,two,three,four"
result_with_limit = text_with_limit.split(',', 2)
print(result_with_limit) # 输出: ['one', 'two', 'three,four']
split() 函数用于将字符串按照指定的分隔符进行分割,并返回一个列表。',' 表示按照逗号分割。maxsplit 参数可以限制分割的次数,分割次数达到 maxsplit 后,剩余的字符串将不再进行分割。上一篇:print在python中用法
下一篇:python能做什么
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站