OnePiece
OnePiece

Reputation: 518

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?

Upvotes: 2

Views: 163

Answers (2)

Timur Shtatland
Timur Shtatland

Reputation: 12347

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 into\t{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', '']

Upvotes: 3

Naga Deepak Kaza
Naga Deepak Kaza

Reputation: 1

That's because in the first case ( is encountered at index 1, so the result of split method will be [a[0:1],a[2:]]

Whereas in the first case ( is encountered at index 0 so split will return [a[0:0],a[0:]]

If you are still confused, consider a string s = "(12(3("

Here ( is encountered at 3 indices 0,3 and 5 so split method returns [s[0:0],s[0+1:3],s[3+1:5],s[5+1:]]

Note: The first and last elements will be something like s[0:i] and s[j:-1] respectively

Upvotes: 0

Related Questions