[Tutor] != -1: versus == 1

Sander Sweers sander.sweers at gmail.com
Fri Jul 17 18:37:25 CEST 2009


2009/7/17 pedro <pedrooconnell at gmail.com>:
> ################################
> for line in theLines:
>   if line.find("Source Height") != -1:
> #etc...
> ###################################
>
> Is there some special reason for this. Why not just write "If it is equal to
> one"

Yes, str.find() returns -1 on failure. See below the documentation for
str.find()

 |  find(...)
 |      S.find(sub [,start [,end]]) -> int
 |
 |      Return the lowest index in S where substring sub is found,
 |      such that sub is contained within s[start:end].  Optional
 |      arguments start and end are interpreted as in slice notation.
 |
 |      Return -1 on failure.

Idle example:

>>> teststr = 'a'
>>> teststr.find('a')
0
>>> teststr.find('b')
-1

Greets
Sander


More information about the Tutor mailing list