Question

Is there any way to break string based on punctuation-word

#!/usr/bin/python

#Asking user to Enter a line in specified format
myString=raw_input('Enter your String:\nFor Example:I am doctor break I stays in CA break   you can contact me on +000000\n')
# 'break' is punctuation word 
<my code which breaks the user input based on break word and returns output in different lists>

Expecting output like

String1:I am doctor

String2:I stays in CA

String2:you can contact me on +000000

Was it helpful?

Solution

A regex based solution, this would also take care of the trailing and leading whitespaces:

>>> import re
>>> text = "I am doctor break I stays in CA break   you can contact me on +000000\n"
>>> re.split(r'\s+break\s+', text)
['I am doctor', 'I stays in CA', 'you can contact me on +000000\n']

OTHER TIPS

You can use the split method on strings as well, which returns a list of all tokens based on the split delimiter.

>>> a="test break testagain break again!"
>>> a.split(" break ")
['test ', ' testagain ', ' again!']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top