Re: shouldn't 'string'.find('ugh') return 0, not -1 ?
- From: Carl Banks <pavlovevidence@xxxxxxxxx>
- Date: Wed, 31 Oct 2007 08:11:04 -0700
On Oct 31, 9:31 am, jelle <jelleferi...@xxxxxxxxx> wrote:
the subject pretty much says it all.
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.
string.find has always been kind of a wart in Python; that's why
they're getting rid of it. For testing for the presence of a
substring, use the in operator:
if "something" in check:
do(somethingElse)
When you need the position of a substring, using the index methods:
i = check.index("something")
index returns an exception of the substring is not there, so no need
to worry about what it returns.
Finally, it really won't kill you to do this:
if check.find("something") >= 0:
do(somethingElse)
Carl Banks
.
- Follow-Ups:
- Re: shouldn't 'string'.find('ugh') return 0, not -1 ?
- From: TheFlyingDutchman
- Re: shouldn't 'string'.find('ugh') return 0, not -1 ?
- References:
- Prev by Date: Re: shouldn't 'string'.find('ugh') return 0, not -1 ?
- Next by Date: Re: help with pyparsing
- 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
|