class Solution: def split(self, value, splitOn): """ :type value: an ascii string, the string that will be split :type splitOn: an ascii Char, which will be used to split value :return type: list[strings] """ if value == None: return None if len(value) == 0: return [""] ans = [] if len(splitOn) == 0: for char in value: ans.append(char) else: start = 0 end = 0 while end < len(value): if value[end] == splitOn: ans.append(value[start:end]) start = end + 1 while start < len(value) and value[start] == splitOn: ans.append("") start += 1 end += 1 ans.append(value[start:end]) return ans def getInput(self): value = raw_input("Please enter a string to split: ") print value splitOn = raw_input("Please enter a character to split on: ") print splitOn result = self.split(value, splitOn) print "Result of splitting " + value + " on " + splitOn + " is " print result if __name__ == "__main__": s = Solution() s.getInput() '''if __name__ == "__main__": s = Solution() print s.split("I love turtles", " ") #['I', 'love', 'turtles'] print s.split("baby", "a") #['b', 'by'] print s.split("Zookeeper", "o") #['Z', '', '', 'keeper'] print s.split(None, "x") #None print s.split("", "x") #[''] print s.split("abcd", "") #['a', 'b', 'c', 'd'] print s.split("abc", "c") #['ab', '']'''
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