Quickly looking at the source, it seems that it str.findis calling stringlib_find_slicefrom here , which ultimately calls fastsearch. The actual algorithm is explained here - with the python pseudocode (which I got from reading the comments).
It looks like the worst case implementation is O (N * M) (same as the naive approach), but can do O (N / M) in some cases (where N and M are the lengths of the string and substring, respectively) and O (N ) in frequent cases 1 .
1 (do not quote me on it - I was only looking at the document)
source
share