# 示例代码:使用 split 方法分割字符串
# 定义一个包含空格的字符串
text = "Hello world this is Python"
# 使用 split 方法按照空格分割字符串,默认情况下,split 会根据任意空白字符(包括空格、制表符、换行符等)进行分割
words = text.split()
# 输出分割后的结果
print(words) # 输出: ['Hello', 'world', 'this', 'is', 'Python']
# 使用 split 方法按照指定字符分割字符串
text_with_commas = "apple,banana,orange,grape"
fruits = text_with_commas.split(',')
# 输出分割后的结果
print(fruits) # 输出: ['apple', 'banana', 'orange', 'grape']
# 指定最大分割次数
text_with_repeated_delimiter = "one,,two,,three"
limited_split = text_with_repeated_delimiter.split(',', 2)
# 输出分割后的结果
print(limited_split) # 输出: ['one', '', 'two,,three']
上一篇:python字符串的操作方法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站