...

/

Splitting Strings Related Tips

Splitting Strings Related Tips

Learn how to split strings efficiently in Python.

str.split() by white spaces

The str.split() method can be called in four ways, though two tend to cause problems. Those are:

  • Without the separator
  • 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 leftovers:

Press + to interact
print('Why, eh, do you, eh, want, eh, it?'.split(', eh, '))

If the split string contains two instances of the separator ...