Home Python Course Menu Book

Python in


The word in is a keyword in Python. It allows you to ask if one string of text is in another string of text. It's normally used with an If Statement. Try this out.

Set up another variable:

searchTerm = "en"

We can now use the searchTerm variable and the myName variable in an If Statement. We can check if the search term is in our string of text:

myName = "Kenny"
searchTerm = "en"
if searchTerm in myName: print("Search term found") else: print("Search term not found")

The only thing you need is the keyword in between your two variables.

Another example is this:

emailAddress = "someemailaddress.com"
if ".com" in emailAddress: print(".com found") else: print(".com not found")

This, however, won't tell you where in the email address the .com is. Just that the text has been found somewhere in the string we were searching.


In the next lesson, we'll take a look at the Python find function.