我刚刚开始学习 python,并且正在使用 slice (:) 运算符和字符串。出于某种原因,如果我指定了一个无效索引,然后是切片运算符,它不会给出“超出范围”错误。
>>> s='Hello'
>>> print s[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> print s[5:]
//Prints just a newline, no error
有人可以解释为什么我在第二种情况下没有出错。