# 示例代码:使用 Python 的 split() 函数
# 定义一个字符串
text = "Hello, world! This is a test."
# 使用 split() 函数将字符串按空格分割成列表
words = text.split()
print("按空格分割:", words)
# 使用 split() 函数指定分隔符(例如逗号)
comma_separated = "apple,banana,cherry"
fruits = comma_separated.split(',')
print("按逗号分割:", fruits)
# 使用 split() 函数限制分割次数
limited_split = text.split(' ', 2)
print("限制分割次数为2:", limited_split)
split()
函数用于将字符串按照指定的分隔符分割成一个列表,默认情况下,分隔符是任意的空白字符(如空格、制表符、换行符等)。,
或者其他字符。maxsplit
来限制分割的最大次数。下一篇:python中转义字符
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站