Discuss / Python / 空的字符串【‘’】为什么用【:1】,不能用【0】

空的字符串【‘’】为什么用【:1】,不能用【0】

Topic source

>>> ''[0]==' '

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

IndexError: string index out of range

>>> ''[:1]==' '

False

再比如:

>>> ''[0]

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

IndexError: string index out of range

>>> ''[:1]

''


  • 1

Reply