8.4. The in and not in operators

The in operator tests if one string is a substring of another:

Note that a string is a substring of itself, and the empty string is a substring of any other string. (Also note that computer scientists like to think about these edge cases quite carefully!)

The not in operator returns the logical opposite result of in.

We can also use the in and not in operators on lists!

However, remember how you were able to check to see if an “a” was in “apple”? Let’s try that again to see if there’s an “a” somewhere in the following list.

Clearly, we can tell that a is in the word apple, and absolutely, and application. For some reason though, the Python interpreter returns False. Why is that? When we use the in and not in operators on lists, Python checks to see if the item on the left side of the expression is equivalent to an element in the item on the right side of the expression. In this case, Python is checking whether or not an element of the list is the string “a” - nothing more or less than that.

You have attempted of activities on this page