python find和index的区别

如果找不到目标元素,index会报错,find会返回-1

>>> s="hello world"
>>> s.find("llo")
2
>>> s.index("llo")
2
>>> s.index("llos")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> s.find("llos")
-1
>>>

posted @ 2020-05-29 16:42  "懒木鱼"  阅读(110)  评论(0编辑  收藏  举报