Bug in string.find; was: Re: Proposed PEP: New style indexing, was Re: Bug in slice type

Bryan Olson fakeaddress at nowhere.org
Thu Aug 25 05:33:10 CEST 2005


The doc for the find() method of string objects, which is
essentially the same as the string.find() function, states:

     find(sub[, start[, end]])
       Return the lowest index in the string where substring sub
       is found, such that sub is contained in the range [start,
       end). Optional arguments start and end are interpreted as
       in slice notation. Return -1 if sub is not found.

Consider:

     print 'Hello'.find('o')

or:

     import string
     print string.find('Hello', 'o')

The substring 'o' is found in 'Hello' at the index -1, and at
the index 4, and it is not found at any other index. Both the
locations found are in the range [start, end), and obviously -1
is less than 4, so according to the documentation, find() should
return -1.

What the either of the above actually prints is:

     4

which shows yet another bug resulting from Python's handling of
negative indexes. This one is clearly a documentation error, but
the real fix is to cure the wart so that Python's behavior is
consistent enough that we'll be able to describe it correctly.


-- 
--Bryan



More information about the Python-list mailing list