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

猜你喜欢

转载自www.cnblogs.com/sea-stream/p/11273317.html