What are other ways to split a string without using the split() method? For example, how could [\'This is a Sentence\'] be split into [\'This\', \'is\', \'a\', \'Sentence\']
You can use regular expressions if you want:
>>> import re
>>> s = 'This is a Sentence'
>>> re.findall(r'\S+', s)
['This', 'is', 'a', 'Sentence']
The \S
represents any character that isn't whitespace, and the +
says to find one or more of those characters in a row. re.findall
will create a list
of all strings that match that pattern.
But, really, s.split()
is the best way to do it.