I just came across this here, always used like this:
if string1.find(string2) <> -1:
pass
What does the <>
operator do, and why not use the usual ==
or in
?
Sorry if that has been answered before, search engines don't like punctuation.
It means not equal to. It was taken from ABC (python's predecessor) see here: x < y, x <= y, x >= y, x > y, x = y, x <> y, 0 <= d < 10. Order tests ( <> means 'not equals')
The less than or equal operator ( <= ) returns true if the left operand is less than or equal to the right operand, and false otherwise.
http://docs.python.org/reference/expressions.html#notin says:
The [operators]
<>
and!=
are equivalent; for consistency with C,!=
is preferred. [...] The<>
spelling is considered obsolescent.
<>
is the same as !=
although the <>
form is deprecated. Your code sample could be more cleanly be written as:
if string2 not in string1:
pass
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With