python中split怎么使用

43次阅读
没有评论

共计 428 个字符,预计需要花费 2 分钟才能阅读完成。

在 Python 中,可以使用字符串的 split() 方法来拆分字符串。split()方法可以根据指定的分隔符将字符串拆分为多个子字符串,并返回一个包含这些子字符串的列表。
以下是 split() 方法的使用方式:

string = "Hello, how are you?"
# 使用空格作为分隔符拆分字符串
words = string.split()
print(words)
# 输出: ['Hello,', 'how', 'are', 'you?']
# 使用逗号作为分隔符拆分字符串
words = string.split(",")
print(words)
# 输出: ['Hello', 'how are you?']
# 使用空格和逗号作为分隔符拆分字符串
words = string.split(",")
print(words)
# 输出: ['Hello', 'how are you?']

split() 方法中,可以传递一个可选的参数作为分隔符。如果不提供分隔符,默认使用空格作为分隔符。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-22发表,共计428字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)