3

I noticed the following weird behavior:

>> str_ = ''  # empty string
>> str_[0:]
''
>> str_[0]
IndexError: string index out of range

Is there some kind of reasoning for this?


My interpretation of the current return value of slicing is:

from position X onwards (and up until Y with a step of Z) there is '' to be found.

which clearly means that there are no characters in the specified interval and thus the string is smaller than (X+1) characters long.

Similarly, one could treat indexing the same way and say:

at position X of string there is '' to be found.

which would also clearly mean that there is nothing there to be found.

but that is not the case. Indexing is treated differently and I am wondering why.

Ma0
  • 15,057
  • 4
  • 35
  • 65

0 Answers0