gpt4 book ai didi

python - 拆分带有附加空格的 Python 字符串(句子)

转载 作者:太空宇宙 更新时间:2023-11-04 08:30:56 25 4
gpt4 key购买 nike

是否可以拆分 Python 字符串(句子),使其在输出中保留单词之间的空格,但在拆分的子字符串中通过将空格附加在每个单词之后?

例如:

given_string = 'This is my string!'
output = ['This ', 'is ', 'my ', 'string!']

最佳答案

我大部分时间都避免使用正则表达式,但在这里它真的很简单:

import re

given_string = 'This is my string!'
res = re.findall(r'\w+\W?', given_string)

# res ['This ', 'is ', 'my ', 'string!']

关于python - 拆分带有附加空格的 Python 字符串(句子),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53044580/

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