Why does substring slicing with index out of range work?

前端 未结 3 1231
栀梦
栀梦 2020-11-22 02:32

Why doesn\'t \'example\'[999:9999] result in error? Since \'example\'[9] does, what is the motivation behind it?

From this behavior I can a

3条回答
  •  情歌与酒
    2020-11-22 03:23

    You're correct! 'example'[3:4] and 'example'[3] are fundamentally different, and slicing outside the bounds of a sequence (at least for built-ins) doesn't cause an error.

    It might be surprising at first, but it makes sense when you think about it. Indexing returns a single item, but slicing returns a subsequence of items. So when you try to index a nonexistent value, there's nothing to return. But when you slice a sequence outside of bounds, you can still return an empty sequence.

    Part of what's confusing here is that strings behave a little differently from lists. Look what happens when you do the same thing to a list:

    >>> [0, 1, 2, 3, 4, 5][3]
    3
    >>> [0, 1, 2, 3, 4, 5][3:4]
    [3]
    

    Here the difference is obvious. In the case of strings, the results appear to be identical because in Python, there's no such thing as an individual character outside of a string. A single character is just a 1-character string.

    (For the exact semantics of slicing outside the range of a sequence, see mgilson's answer.)

提交回复
热议问题