I'm currently trying to replicate the split function. I have looked around and found one that has helped a lot, the only problem with the code is the empty string is not displayed empty rather they have multiple Apostrophe for multiple different empty strings. the current code is
def mysplit(strng):
words = []
current_word = " "
for char in strng:
if char == " ":
words.append(current_word)
current_word = ""
else:
current_word += char
words.append(current_word)
return words
print(mysplit("To be or not to be, that is the question"))
print(mysplit("To be or not to be,that is the question"))
print(mysplit(" "))
print(mysplit(" abc "))
print(mysplit(""))
The output that comes out
[' To', 'be', 'or', 'not', 'to', 'be,', 'that', 'is', 'the', 'question']
[' To', 'be', 'or', 'not', 'to', 'be,that', 'is', 'the', 'question']
[' ', '', '', '']
[' ', 'abc', '']
[' ']
I'm trying to get the outcome of
['To', 'be', 'or', 'not', 'to', 'be,', 'that', 'is', 'the', 'question']
['To', 'be', 'or', 'not', 'to', 'be,that', 'is', 'the', 'question']
[]
['abc']
[]
current_wordwith an empty string not a space. Then, if you only append towordswhen current_word is not empty, you should obtain what you are looking for (which is not exactly how split() works btw) - Alain T.