• Home
  • 4.14 Membership Operators

4.14 Membership Operators

View Categories

4.14 Membership Operators

4 min read

Membership operators are used to check whether a character or substring exists within a string.

Python provides two membership operators:

  • in
  • not in

The in operator returns True if the specified character or substring is present in the string. The not in operator returns True if the specified character or substring is absent.

Membership operators are commonly used for searching text, validating user input, checking keywords, and making decisions in programs.


Syntax #

substring in string_name

or

substring not in string_name

Components #

Component Description
substring Character or sequence of characters to search for.
in Returns True if the substring exists.
not in Returns True if the substring does not exist.
string_name The string being searched.

Example 1: Using the in Operator #

main.py

text = "Python Programming"

print("Python" in text)

Output #

True

Explanation #

  • Python searches for the substring "Python".
  • The substring exists in the string.
  • Therefore, the result is True.

Example 2: Using the not in Operator #

main.py

text = "Python Programming"

print("Java" not in text)

Output #

True

Explanation #

  • Python searches for "Java".
  • The substring is not found.
  • Therefore, not in returns True.

Example 3: Searching for a Character #

main.py

word = "Computer"

print("m" in word)

Output #

True

Explanation #

  • Membership operators work with single characters as well.
  • The character "m" exists in "Computer".
  • Therefore, the result is True.

Example 4: Case-Sensitive Search #

main.py

text = "Python"

print("python" in text)

Output #

False

Explanation #

  • Membership operations are case-sensitive.
  • "Python" and "python" are different strings.
  • Therefore, the result is False.

Example 5: Searching for a Word #

main.py

sentence = "Python is easy to learn."

print("easy" in sentence)

Output #

True

Explanation #

  • Python searches for the substring "easy".
  • The word is found in the sentence.
  • Therefore, the result is True.

Example 6: User Input #

main.py

email = input("Enter your email: ")

print("@" in email)

Sample Input #

alice@example.com

Output #

True

Explanation #

  • The user enters an email address.
  • The program checks whether it contains the @ character.
  • Since it exists, the result is True.

Example 7: Searching for a Numeric String #

main.py

number = "987654321"

print("654" in number)

Output #

True

Explanation #

  • "654" is treated as a substring.
  • Python finds it within the string.
  • Therefore, the result is True.

Example 8: Using not in #

main.py

word = "Python"

print("z" not in word)

Output #

True

Explanation #

  • Python searches for the character "z".
  • It is not found.
  • Therefore, not in returns True.

Example 9: Case-Insensitive Membership Test #

main.py

text = "Python Programming"

print("python" in text.lower())

Output #

True

Explanation #

  • lower() converts the entire string to lowercase.
  • "python" is then searched in the lowercase string.
  • This performs a case-insensitive membership test.

Example 10: Checking the Result Type #

main.py

result = "Py" in "Python"

print(result)
print(type(result))

Output #

True
<class 'bool'>

Explanation #

  • Membership operators return either True or False.
  • Therefore, the result is of type bool.

Common Mistakes #

1. Assuming Membership Checks Ignore Case #

Incorrect

print("python" in "Python")

Incorrect Expectation #

True

Actual Output #

False

Reason

Membership operators are case-sensitive.

Correct

print("python" in "Python".lower())

2. Confusing Characters with Numbers #

Incorrect

print(5 in "12345")

Output #

TypeError: 'in <string>' requires string as left operand, not int

Reason

The left operand must be a string when searching within another string.

Correct

print("5" in "12345")

3. Expecting in to Return the Position #

Incorrect

print("Py" in "Python")

Incorrect Expectation #

0

Actual Output #

True

Reason

The in operator only checks for existence.
It does not return the position of the substring.


4. Assuming Only Whole Words Can Be Found #

Incorrect

print("gram" in "Programming")

Incorrect Expectation #

False

Actual Output #

True

Reason

Membership operators search for any matching substring, not just complete words.


Best Practices #

  • Use in to check whether a substring exists before processing it.
  • Remember that membership operations are case-sensitive.
  • Use lower() or upper() for case-insensitive searches.
  • Use descriptive variable names for the string and the substring being searched.
  • Use not in when checking that a substring is absent.

Key Points to Remember #

  • Python provides the in and not in membership operators.
  • in returns True if a substring exists in a string.
  • not in returns True if a substring does not exist.
  • Membership operations are case-sensitive.
  • They work with both characters and substrings.
  • The result is always of type bool.
  • Membership operators check for existence, not the position of the substring.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *