用于拆分单词

value = "HELLO HOW ARE YOU WHAT ARE YOU DOING"
result = value.split(" ",2)

>> output will be:['HELLO','HOW', 'ARE YOU WHAT ARE YOU DOING']


但是如果我想要这样的结果怎么办

output: ['HELLO HOW ARE YOU WHAT ARE' ,'YOU' ,'DOING']

最佳答案

使用rsplit

value = "HELLO HOW ARE YOU WHAT ARE YOU DOING"
result = value.rsplit(" ", 2)
print(result) # ['HELLO HOW ARE YOU WHAT ARE', 'YOU', 'DOING']

关于python - 使用最后2个空格分割字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59045980/

10-12 16:56