简体   繁体   中英

String Split into separate Strings in python

I want to split "Onehundredthousand" to "one" "hundred" "thousand" using python. How can I do that?

You can use a string's partition method to split it into 3 parts (left part, separator, right part):

"onehundredthousand".partition("hundred")
# output: ('one', 'hundred', 'thousand')
>>> s = "Onehundredthousand"
>>> s.replace('hundred', '_hundred_').split('_')
['One', 'hundred', 'thousand']

This will only work on the given string.

Using regular expression re.split . If you use captured group as a separator, it will be also included in the result list:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM