[Python-Dev] Dafanging the find() gotcha

Patrick K. O'Brien pobrien@orbtech.com
Mon, 5 Aug 2002 19:05:53 -0500

[Tim Peters]
>     s2.find(s1)
> returns the smallest non-negative int i such that
>     s2[i : i+len(s1)] == s1  # Fixed typo in original.
> provided such an i exists.  That's as sensible as any answer, and more
> sensible than most <wink>, if you have to give a meaning when s1 is empty.

That clarified things for me, thanks.

(But if you squint while thinking about finding single characters and using
the result to access the original string via index notation, rather than
slice, and you ignore the fact that '' isn't even a single character and you
do this late in the day... you might see why I wasn't seeing the logic of
'whatever'.find('') returning 0.)

Patrick K. O'Brien
"Your source for Python programming expertise."
Web:  http://www.orbtech.com/web/pobrien/
Blog: http://www.orbtech.com/blog/pobrien/
Wiki: http://www.orbtech.com/wiki/PatrickOBrien