当对连续分割项的字符串使用.split()时,为什么会有空格?

I noticed that when I did "heelo".split("e"), it would return ['h', '', 'lo']. Why is there an empty/a whitespace item in the list? Shouldn't it have been ['h', 'lo']?

I am confused on why I received that result, instead of what I had expected and would appreciate if someone could explain me the functionality of split better.

评论
iodit
iodit

From the Python docs:

如果给定sep,则不将连续的定界符分组在一起,而是将其视为定界空字符串(例如,'1,,2'.split(',')返回['1','','2'])

Your string is divided between the first e and second e, but there is no character there, so you get an empty character back ''

点赞
评论