# 1 Gold Star # The built-in <string>.split() procedure works # okay, but fails to find all the words on a page # because it only uses whitespace to split the # string. To do better, we should also use punctuation # marks to split the page into words. # Define a procedure, split_string, that takes two # inputs: the string to split and a string containing # all of the characters considered separators. The # procedure should return a list of strings that break # the source string up by the characters in the # splitlist. def split_string(source,splitlist): string = '' stringList = [] shouldAdd = True for e in source: shouldAdd = True for c in splitlist: if e==c: if string != '': stringList.append(string) string= '' shouldAdd= False if shouldAdd: string = string + e if string != '': stringList.append(string) return stringList out = split_string("This is a test-of the,string separation-code!"," ,!-") print out #>>> ['This', 'is', 'a', 'test', 'of', 'the', 'string', 'separation', 'code'] out = split_string("After the flood ... all the colors came out.", " .") print out #>>> ['After', 'the', 'flood', 'all', 'the', 'colors', 'came', 'out'] out = split_string("First Name,Last Name,Street Address,City,State,Zip Code",",") print out #>>>['First Name', 'Last Name', 'Street Address', 'City', 'State', 'Zip 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