python中find和index的区别


find 和 index 都是用来搜索目标字符串的位置

区别:

如果目标字母不存在,find 返回 -1,index 返回异常

a='sdfdjfjofe'
a.find("d")
1
a.index("d")
1
a.find("x")
-1
a.index("x")
Traceback (most recent call last):
  File "", line 1, in 
ValueError: substring not found