If we run .split() without an argument, what happens to consecutive whitespaces?


#1

Question

In the context of this exercise, if we run .split() without an argument, what happens to consecutive whitespaces?

Answer

When you don’t provide an input argument for .split(), the function will treat consecutive whitespaces as though they are a single separator.

For example,

string= "a a  a   a"
words = string.split()

print(words)
# ['a', 'a', 'a', 'a']

If instead, we ran this with an argument of a single whitespace character " ", the result would be different, because we are splitting between every whitespace character, even resulting in some empty characters in our list.

string = "a a  a   a"
words = string.split(" ")

print(words)
# ['a', 'a', '', 'a', '', '', 'a']