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
>>> 

 

参考:

https://blog.csdn.net/yolandera/article/details/80264876

 

 

posted @ 2019-07-30 22:50  anobscureretreat  阅读(1034)  评论(0编辑  收藏  举报