我刚刚开始学习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
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下为什么我在第二种情况下没有出错。
| 归档时间: |
|
| 查看次数: |
1267 次 |
| 最近记录: |