Re: shouldn't 'string'.find('ugh') return 0, not -1 ?
- From: Tim Chase <python.list@xxxxxxxxxxxxxxxxx>
- Date: Wed, 31 Oct 2007 10:13:44 -0500
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
.
- Prev by Date: Re: help with pyparsing
- Next by Date: Re: Python bug tracker now secret?
- Previous by thread: Re: shouldn't 'string'.find('ugh') return 0, not -1 ?
- Next by thread: Re: shouldn't 'string'.find('ugh') return 0, not -1 ?
- Index(es):
Relevant Pages
|