HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

9) Operators and Booleans Lesson

Python in Operator

8 min to complete · By Martin Breuss

In the section about strings and text, you learned how to find substrings in a string using string methods. This worked well for looking for file extensions, such as .jpg, with the string method str.endswith(). But what if you just want to know whether a substring is anywhere inside a text you've been handed?

Python's membership operator in can help with that!

How Does the Membership Operator Work

Imagine your favorite Latin teacher proudly presents you with the newest homework sheet, gently placing it in front of your eyes with the words:

Is the word euismod in this text?

They deliver this sentence with the most charming smile. You lower your gaze to the worksheet and notice how a cold horror creeps up somewhere in the vicinity of your spine. What you see is this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sem massa, imperdiet quis feugiat nec,
maximus vitae justo. Ut a sem nulla. Ut pulvinar, libero sollicitudin lacinia faucibus, sapien mauris
viverra nibh, eget blandit leo nibh ut neque. Pellentesque bibendum est quam, quis dictum eros placerat
viverra. Nunc ac ultricies lacus. Suspendisse mollis sed diam feugiat bibendum. Vivamus imperdiet lacus
velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed
quam augue, volutpat ac nisi at, vestibulum sodales augue.

Ut varius magna at viverra rutrum. Sed rhoncus accumsan placerat. Maecenas sed pretium mauris. Sed tempus
molestie sagittis. Proin bibendum dignissim tortor sed vestibulum. Etiam dignissim eros sed sem porttitor,
non volutpat dui cursus. Nullam sit amet arcu porta, aliquet ante eget, venenatis felis. Duis sit amet
aliquet tortor. Ut euismod molestie tellus ac lacinia. Ut tincidunt, tellus a rutrum semper, ex orci
convallis libero, in accumsan ante lectus nec odio. Ut cursus fringilla nibh et bibendum.

Vestibulum ipsum velit, porttitor gravida tristique at, facilisis eu leo. Suspendisse ac nibh eros. Mauris
sem ligula, sagittis eu purus nec, pellentesque faucibus nisi. Sed pulvinar augue nec mauris dictum, sit
amet efficitur mi aliquam. Donec eu porttitor risus, suscipit laoreet erat. Duis purus magna, venenatis a
placerat hendrerit, suscipit sit amet est. Donec luctus efficitur arcu nec rutrum. Vestibulum suscipit enim
molestie libero mollis, in egestas est consequat. Integer urna dui, mollis vitae viverra a, gravida eu nisi.

Sheesh! Okay, time to start reading some Latin...

Or maybe there's a faster way? Ask Python for help by simply passing the question along:

text = "Lorem ipsum dolor ---- snip, but you should put it all in here! ---- a, gravida eu nisi."

"euismod" in text  # True

Welp, that was fast! Thanks, Python!

What is a Membership Operator

What you used to get this swift answer is called the membership operator in. It sounds quite intuitive, right? That is because reading Python code is sometimes nearly like reading English. You've heard that one before, haven't you? :)

How to Use the Python in Operator

The membership operator in allows you to quickly check whether a string contains a substring at any position. Python doesn't tell you where the substring is, but you also didn't ask for that.

Colorful illustration of a light bulb

Info: The membership operator (in) is an intuitive way to check whether an element is part of a collection.

In this case, you checked whether a substring is part of a longer string. Similarly, you can use the second membership operator (not in) to check whether an element is not in a collection:

"euismod" not in text  # False

In your REPL session, play around some more with using in and not in and looking for words in the Latin text:

Playground: Practice with the in Operator

  • Is the word pulvinar in the text?
  • What about tincidunc?
  • What about Proin? Remember that capitalization matters!
  • Can you think of a way to generalize the search so that you won't get tripped up by different capitalization of words?
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
textContains = "consectetur" in text
print(textContains)

You can read a deep dive into the topic of checking for substrings in strings in my article on how to check if a Python string contains a substring.

Once you're fed up asking Python whether words are part of Lorem Ipsum, you might start to wonder what exactly are those answers that Python keeps giving you when you ask whether a substring is in a larger string.

Boolean Values

Python seems to be quite bi-syllabic about it. It always keeps giving you only one of two answers:

  • True
  • False

These values are another Python data type, and they are at the core of how all computers are built. They are boolean values and you'll learn more about them in the upcoming lesson.

Summary: Python in Operator

  • Python in operator checks whether an element exists in a collection
  • The in operator is useful for search for word(s) in a long piece of text
  • The in operator returns a boolean value, which is either True or False