JavaScript
x
6
1
a = "1)2"
2
b = ")"
3
a = a.split(")")
4
b = b.split(")")
5
print(a, len(a), b, len(b))
6
returns
JavaScript
1
2
1
['1', '2'] 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.
JavaScript
1
6
1
lst = ['1', ')', '1)', ')2', '1)2', '1)2)', '))', ')1)2)']
2
3
for s in lst:
4
s_split = s.split(')')
5
print(f'"{s}" is split intot{len(s_split)} element(s):t', s_split)
6
Prints:
JavaScript
1
9
1
"1" is split into 1 element(s): ['1']
2
")" is split into 2 element(s): ['', '']
3
"1)" is split into 2 element(s): ['1', '']
4
")2" is split into 2 element(s): ['', '2']
5
"1)2" is split into 2 element(s): ['1', '2']
6
"1)2)" is split into 3 element(s): ['1', '2', '']
7
"))" is split into 3 element(s): ['', '', '']
8
")1)2)" is split into 4 element(s): ['', '1', '2', '']
9