shouldn't 'string'.find('ugh') return 0, not -1 ?

jelle feringa jelleferinga at
Wed Oct 31 16:43:29 CET 2007

>  The statement that you want to test the truth of is s.find(q) >= 0.  In
> other words, you want to see that the substring was found at a valid
> (non-negative) location. As someone else pointed out, it would make more
> sense to use None instead of -1.

I agree, that would be nice.

  You still couldn't use `if s.find(q):` because you'd get a false result
> for a substring found at the beginning of the string, but the return value
> would make more intuitive sense.

I'm sorry, but is that so?
>>>None == 0

Since the start of the string is indicated by 0 and not None, that should
work, right?

Witness also the problem of actually using the result of find:
> s[s.find(q)]
> will yield a valid part of the string (the last character) when q is not
> found.  If it evaluated to none, you'd get a respectable TypeError instead.
> But you still wouldn't--and never will--be able to say `if s.find(q)`,
> because valid array indices can be either true or false.

Yes, but that is the issue here. If the substring is not found, still find()
returns -1, which is a valid indice.
That I think is ugly. I can't think of a case where it would be prefereble
to have find() return -1 rather than None.
Formally speaking, -1 is an ambigious answer, since a perfectly valid

the '_'.find(' ') might actually make a nice idiom while parsing for
ignoring irrelevant lines, and processing lines that are of interest in the
conditional expression.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list