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

jelle feringa jelleferinga at gmail.com
Wed Oct 31 16:32:49 CET 2007


Hi Tim,

Well, I this is another idiom in itself, right?
Your checking if something is part of an iterable.
I'm checking truth before entering a conditional expression.
The latter is considered to be pythonic, right?

-jelle


 On 10/31/07, Tim Chase <python.list at tim.thechases.com> wrote:
>
> > if I check a string for for a substring, and this substring isn't found,
> > should't the .find method return 0 rather than -1?
> > this breaks the
> >
> > if check.find('something'):
> >     do(somethingElse)
> >
> > idiom, which is a bit of a pity I think.
>
> That idiom is spelled:
>
>   if 'something' in check:
>     do(somethingElse)
>
> Unless you really do need to start at a particular offset where
> you use the additional parameters of find(), such as
> find('something', 3, 42).  In that case, you can slice your target:
>
> if 'something' in check[3:42]:
>    do(somethingElse)
>
> The above is untested, so check for fencepost errors, but the
> theory holds.
>
> So pretty much, I'd never consider using find() :)
>
> -tkc
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071031/400393ba/attachment.html>


More information about the Python-list mailing list