2

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

2
  • 1
    post ur attempts.. Commented Apr 22, 2016 at 6:28
  • 2
    Just for this particular string,there can n different solutions. But if you want a generic solution,u need to have some delimiters. Commented Apr 22, 2016 at 7:05

3 Answers 3

6

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')
Sign up to request clarification or add additional context in comments.

Comments

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

This will only work on the given string.

1 Comment

Using replace and then split? It's better to use partition.
4

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']

1 Comment

Thanks for your support

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.