Python Tip #76 (of 365):

Don't call the string split() method with a space character

Instead of this:

words = some_string.split(" ")

Do this:

words = some_string.split()

Calling the split method without arguments will:

• Trim whitespace from either end of the string
• Split by any amount of consecutive whitespace characters

Note that "whitespace characters" isn't just spaces: newlines and tabs count also.

More on the split() method: https://pym.dev/string-split-method/

#Python #DailyPythonTip

The string split method in Python

Strings can be split by a substring separator. Usually the string split is called without any arguments, which splits on any whitespace.