【发布时间】:2013-11-23 16:13:37
【问题描述】:
我刚刚开始学习 python,我正在玩切片 (:) 运算符和字符串。出于某种原因,如果我在切片运算符后指定了一个无效索引,它不会给出“超出范围”错误。
>>> 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
谁能解释一下为什么我在第二种情况下没有出错。
【问题讨论】:
-
不,切片永远不会导致越界异常。