Python中是否有一个函数来分割字符串而不忽略空格?

gat*_*ath 12 python split

Python中是否有一个函数来分割字符串而不忽略结果列表中的空格?

例如:

s="This is the string I want to split".split()
Run Code Online (Sandbox Code Playgroud)

给我

>>> s
['This', 'is', 'the', 'string', 'I', 'want', 'to', 'split']
Run Code Online (Sandbox Code Playgroud)

我想要类似的东西

['This',' ','is',' ', 'the',' ','string', ' ', .....]
Run Code Online (Sandbox Code Playgroud)

Gre*_*ill 41

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

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