[Python-Dev] Dafanging the find() gotcha

Tim Peters tim.one@comcast.net
Mon, 05 Aug 2002 19:33:37 -0400


[Patrick K. O'Brien]
> In what way does find('') return a sensible answer?
>
> >>> 'help'.find('')
> 0
> >>> 'help'.find('h')
> 0
> >>> 'help'.find('e')
> 1
> >>> 'help'.find('l')
> 2
> >>> 'help'[0]
> 'h'
> >>> 'help'[1]
> 'e'
> >>> 'help'[2]
> 'l'
> >>> s = 'help'
> >>> s[s.find('')]
> 'h'
> >>> s[s.find('h')]
> 'h'
>
> I don't see the logic in this

In what?  The meaning of "this" isn't clear.  Do you mean that not a single
one of those results makes sense to you, or that some particular ones don't
make sense to you?  If the latter case, which particular ones?

Note that searching for any prefix of 'help' returns 0:

>>> 'help'.find('help')
0
>>> 'help'.find('hel')
0
>>> 'help'.find('he')
0
>>> 'help'.find('h')
0
>>> 'help'.find('')
0
>>>

Of course '' is a prefix of any string whatsoever, so it's not like the
final result is of much use (it's more like "no information in, no
information out").