def split_string(source, splitlist): output = [] atsplit = True for char in source: if char in splitlist: atsplit = True print output else: if atsplit: output.append(char) atsplit = False print output else: #add character to last word output[-1] = output[-1] + char return output out = split_string("This is a test-of the, separation code", " ,!-") print out ['T'] ['This'] ['This', 'i'] ['This', 'is'] ['This', 'is', 'a'] ['This', 'is', 'a'] ['This', 'is', 'a', 't'] ['This', 'is', 'a', 'test'] ['This', 'is', 'a', 'test', 'o'] ['This', 'is', 'a', 'test', 'of'] ['This', 'is', 'a', 'test', 'of', 't'] ['This', 'is', 'a', 'test', 'of', 'the'] ['This', 'is', 'a', 'test', 'of', 'the'] ['This', 'is', 'a', 'test', 'of', 'the', 's'] ['This', 'is', 'a', 'test', 'of', 'the', 'separation'] ['This', 'is', 'a', 'test', 'of', 'the', 'separation', 'c'] ['This', 'is', 'a', 'test', 'of', 'the', 'separation', 'code']
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question