首页 > 解决方案 > 为什么 str[64:] 有效而 str[64] 无效?

问题描述

假设您有一个名为 s 的字符串:

s = "000000000000000000000000000000000000000000000000000000000000000"
len(s)
#63

当您运行s[64:]时,Python 编译器会返回''
但是,当您运行s[64]代码时会引发以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

为什么会这样?
提前致谢。

标签: pythonstringindexing

解决方案


推荐阅读