>> s -6ren">
gpt4 book ai didi

python - Python 中是否有一个函数可以在不忽略空格的情况下拆分字符串?

转载 作者:太空狗 更新时间:2023-10-29 17:35:46 25 4
gpt4 key购买 nike

Python 中是否有一个函数可以在不忽略结果列表中的空格的情况下拆分字符串?

例如:

s="This is the string I want to split".split()

给我

>>> s
['This', 'is', 'the', 'string', 'I', 'want', 'to', 'split']

我想要类似的东西

['This',' ','is',' ', 'the',' ','string', ' ', .....]

最佳答案

>>> import re
>>> re.split(r"(\s+)", "This is the string I want to split")
['This', ' ', 'is', ' ', 'the', ' ', 'string', ' ', 'I', ' ', 'want', ' ', 'to', ' ', 'split']

在 re.split() 中使用捕获括号会导致函数也返回分隔符。

关于python - Python 中是否有一个函数可以在不忽略空格的情况下拆分字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/113534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com