September 2021
Intermediate to advanced
152 pages
3h 5m
English
| Tip 99 | str.split() by White Spaces |
★★2.7, 3.4+ Method str.split can be called two ways (actually, four ways, but the other two usually do not cause any confusion or trouble): without the separator and with a separator. When the method is called with a separator, the method “removes” the separator string as a whole (not the individual separator characters) from the split string and returns a list of the leftovers:
| | 'Why, eh, do you, eh, want, eh, it?'.split(', eh, ') |
| => | ['Why', 'do you', 'want', 'it?'] |
If the split string contains two instances of the separator side by side and the method removes them, there is an invisible empty string between them that makes its way onto the list of the leftovers:
| | 'Mary had a little lamb'.split( ... |
Read now
Unlock full access