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

Ian Kelly ian.g.kelly at gmail.com
Mon Jul 13 23:12:18 EDT 2015


On Mon, Jul 13, 2015 at 10:56 AM, Roel Schroeven <roel at roelschroeven.net> 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.

There was a long thread from 2005 on python-dev proposing to remove
str.find in Python 3, but evidently it didn't make the cut:
https://mail.python.org/pipermail/python-dev/2005-August/055704.html



More information about the Python-list mailing list