Short review: index and find
There is also an index next to the find method. find and index both give the same result: they return the position of the first occurrence,, but if nothing is found, index will raise a ValueError , while find return -1 , In speed both have the same test results.
s.find(t) #returns: -1, or index where t starts in s s.index(t) #returns: Same as find, but raises ValueError if t is not in s
Additional knowledge: rfind and rindex :
In the general case, find and index return the smallest index where the line with the transfer is run, and rfind and rindex returns the largest index to which it runs Most string search algorithms look from left to right , so functions starting with r show that the search comes from from right to left .
Therefore, if the likelihood that the item you are looking for is close to the end than to the top of the list, rfind or rindex will be faster.
s.rfind(t) #returns: Same as find, but searched right to left s.rindex(t) #returns: Same as index, but searches right to left
Source: Python: Visual QuickStart Guide, Toby Donaldson
user1767754 Dec 19 '17 at 5:31 on 2017-12-19 05:31
source share