I understand the working of the find and index methods -
str = "this is bad"
str.find("good")
-1
str.index("good")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
But what is the rationale behind having two functions for the same task?
I found this as a good answer and understood that with
find() - if the string isn't found, -1 that is returned is the same as the index of the last character in the string as so would create confusion
index() - the try-catch that would have to be used would create an overhead for a fast operation
or am I getting this wrong?
Also, why if that the case, the find()
method can only be used with strings?
Surely, the overhead using index()
is the same whether you use it on lists or strings? Or is it because lists tend to be generally bigger in size than strings?