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 "<input>", line 1, in <module>
ValueError: substring not found

 

posted @ 2022-02-11 15:37  keep2021  阅读(639)  评论(0编辑  收藏  举报