Skip to content
Advertisement

Python split function behaviour

a = "1)2"
b = ")"
a = a.split(")")
b = b.split(")")
print(a, len(a), b, len(b))

returns

['1', '2'] 2 ['', ''] 2

This behaviour seems really strange to me. Why are blanks returned only for b and not a?

Advertisement

Answer

As was pointed out by others, the documented behavior of str.split explains your results. Since you specify sep to be ')', split looks for the strings that surround it, and in the case of ')', finds exactly 2 empty strings (not blanks). In the case of '1)2', split finds 2 non-empty strings ('1' and '2'). Note that this behavior is extended to other similar cases, see below. As you can see, split, when provided with sep, returns empty strings in cases when the sep occur consecutively, or at the beginning or the end of a string.

lst = ['1', ')', '1)', ')2', '1)2', '1)2)', '))', ')1)2)']

for s in lst:
    s_split = s.split(')')
    print(f'"{s}" is split intot{len(s_split)} element(s):t', s_split)

Prints:

"1" is split into       1 element(s):    ['1']
")" is split into       2 element(s):    ['', '']
"1)" is split into      2 element(s):    ['1', '']
")2" is split into      2 element(s):    ['', '2']
"1)2" is split into     2 element(s):    ['1', '2']
"1)2)" is split into    3 element(s):    ['1', '2', '']
"))" is split into      3 element(s):    ['', '', '']
")1)2)" is split into   4 element(s):    ['', '1', '2', '']
Advertisement