在python中拆分为单独字符串的字符串

2024-10-03 02:37:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用python将"Onehundredthousand"拆分为"one""hundred""thousand"。 我该怎么做?你知道吗


Tags: onethousandhundredonehundredthousand
3条回答

使用正则表达式^{}。如果使用捕获的组作为分隔符,它也将包含在结果列表中:

>>> import re
>>> re.split('(hundred)', 'Onehundredthousand')
['One', 'hundred', 'thousand']
>>> s = "Onehundredthousand"
>>> s.replace('hundred', '_hundred_').split('_')
['One', 'hundred', 'thousand']

这只适用于给定的字符串。你知道吗

可以使用字符串的partition方法将其拆分为3部分(左部分、分隔符、右部分):

"onehundredthousand".partition("hundred")
# output: ('one', 'hundred', 'thousand')

相关问题 更多 >