Difference between find and index function in Python . | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Difference between find and index function in Python .

3rd Apr 2020, 7:33 PM
Rishi
Rishi - avatar
2 Answers
+ 1
str.find returns -1 when it does not find the substring. >>> line = 'hi, this is ABC oh my god!!' >>> line.find('?') -1 While str.index raises ValueError: >>> line.index('?') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found Both the functions behave the same way if a sub-string is found. source: https://stackoverflow.com/a/22190078
3rd Apr 2020, 8:11 PM
visph
visph - avatar
0
visp@ thanks
7th Apr 2020, 1:35 PM
Rishi
Rishi - avatar