Discuss / Python / 为啥不能用索引s[0]的方式

为啥不能用索引s[0]的方式

Topic source

*小小

#1 Created at ... [Delete] [Delete and Lock User]

发现对于空字符串,s[0]的写法会字符越界,为啥呢,其他非空字符串都正常

Kang

#2 Created at ... [Delete] [Delete and Lock User]

在Python中,字符串索引是从0开始的。空字符串没有字符可以索引。

print(len('')) # 0

获取s[0]的写法肯定会字符越界


  • 1

Reply