# 示例代码:split函数的用法
# split() 方法用于将字符串按照指定的分隔符进行分割,并返回一个列表。
# 语法:str.split(separator, maxsplit)
text = "Hello, this is a test string."
# 使用逗号作为分隔符
result1 = text.split(",") # ['Hello', ' this is a test string.']
print("使用逗号作为分隔符:", result1)
# 使用空格作为分隔符
result2 = text.split(" ") # ['Hello,', 'this', 'is', 'a', 'test', 'string.']
print("使用空格作为分隔符:", result2)
# 指定最大分割次数
result3 = text.split(" ", 3) # ['Hello,', 'this', 'is', 'a test string.']
print("指定最大分割次数:", result3)
# 不指定分隔符,默认为空格,连续的空格会被视为一个分隔符
result4 = text.split() # ['Hello,', 'this', 'is', 'a', 'test', 'string.']
print("不指定分隔符:", result4)
上一篇:python re.split
下一篇:python的print()函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站