[Tutor] Meaning of -1 in Python
Terry Carroll
carroll at tjc.com
Sat Dec 11 02:37:16 CET 2010
On Fri, 10 Dec 2010, Alan Gauld wrote:
> "Ben Ganzfried" <ben.ganzfried at gmail.com> wrote
>
>> n = s.find('not')
>> b = s.find('bad')
>> if n != -1 and b != -1 and b > n:
>> s = s[:n] + 'good' + s[b+3:]
>> return s
>>
>> It's clear that n!=-1 and b!=-1 means something like : "if in the
>> string 's' we find the word "not" and in string 's' we find the word
>> "bad."
>
> Exactly the oopsite in fact.
> find() returns the index where the string is fouind.
> -1 means the string was not found
I don't think it's opposite; I think Ben and Alan are saying the same
thing, albeit in different ways.
Ben:
n = s.find('not')
n!=-1 ... means something like : "if in the string 's' we find the word
"not"
Yes; "n != -1" means "not not found", that is, found.
(ditto for the discussion of "b!=-1")
Alan:
-1 means the string was not found
Exactly. Both of you are saying that -1 means "not found"; Ben is
expresing that negating that is equivalent to "found."
More information about the Tutor
mailing list