Elegant check for -1 in Python

python, string

Solution

Don't use `str.find()` if you don't need the index. Use `in` to test for the substring instead; or in this case `not in` to negate the test:

if 'foo' not in myString:
    print("foo not found in", myString)

Problem

Is there an elegant way to check for the value -1 as returned by the `str.find()` instance method ? I find testing for a value of -1 very ugly but I don't want to say < 0 either as that could cause confusion. ``` if (myString.find('foo') == -1): print("foo not found in ", myString) ```

Original source