str.index() and str.find() versus only list.index()

Steven D'Aprano steve at
Tue Jul 14 05:23:44 CEST 2015

On Tue, 14 Jul 2015 01:12 pm, Ian Kelly wrote:

> On Mon, Jul 13, 2015 at 10:56 AM, Roel Schroeven <roel at>
> wrote:
>> Hi,
>> Quick question: why does str have both index() and find(), while list
>> only has index()? Is there a reason for that, or is it just an historical
>> accident?
> Historical accident, I think. If it were to be redone, I doubt that
> str.find would exist. The problem with it is that it returns -1 to
> indicate that the argument was not found, but -1 is a valid index into
> the string. This is a potential source of hard-to-find bugs.

Correct. But rather than removing it, it would be better to take a leaf out
of re.match's book and return None as the sentinel. That would eliminate
the "using -1 as a valid index" bug.


More information about the Python-list mailing list