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

时间:2016-04-22 06:19:35

标签: python

我想使用python将"Onehundredthousand"分割为"one" "hundred" "thousand"。 我怎么能这样做?

3 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:5)

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

这只适用于给定的字符串。

答案 2 :(得分:4)

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

>>> import re
>>> re.split('(hundred)', 'Onehundredthousand')
['One', 'hundred', 'thousand']