'Understanding split() function in python

Why does this code return 3 strings in the resulting list?

def cat_dog(characters):
    n1 = characters.split('cat')
    print(n1)
    return len(n1)

On executing the code, I get the following. Why do I have 3 strings in my list? I'm trying to understanding how the split() method works. I would expect to see just 2 strings since I'm splitting at cat for the string catcat -- not sure why there are 3 strings in my new list after splitting.

cat_dog('catcat')

['', '', '']

3


Solution 1:[1]

According to the official documentation:

string.split(s[, sep[, maxsplit]]): If the second argument sep is present and not None, it specifies a string to be used as the word separator.

So characters.split('cat') will return an array where the string is separated by the word cat. if you will call "1cat2cat3".split('cat'), you will get ["1", "2", "3"].

In your case, the string catcat can be represented as '' + 'cat' + '' + 'cat' + '', so 'catcat'.split('cat') will return ['', '', ''].

Solution 2:[2]

The python documentation should answer your question:

Quoted here:

...delimiters... are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']).

In your case, the string catcat is treated as:

'<empty_str>cat<empty_str>cat<empty_str>' with cat as delimiter.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 J. Scott Elblein