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



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


.



Relevant Pages