Geek University

Membership operators

The membership operators in Python are used to test whether a value is found within a sequence. For example, you can use the membership operators to test for the presence of a substring in a string. Two membership operators exist in Python:

  • in – evaluates to True if the value in the left operand appears in the sequence found in the right operand. For example, ‘Hello’ in ‘Hello world’ returns True because the substring Hello is present in the string Hello world!.
  • not in – evaluates to True if the value in the left operand doesn’t appear in the sequence found in the right operand. For example, ‘house’ in ‘Hello world’ returns True because the substring house is not present in the string Hello world!.

 

Here are a couple of examples:

>>> 'Hello' in 'Hello world!'
True
>>> 
>>> 'Hello' in 'Hello world!'
True
>>> 'house' in 'Hello world!'
False
>>> 'hello' in 'Hello world!'
False // note that Python is case sensitive
>>> 'orld' in 'Hello world!'
True
>>> 'Hello' not in 'Hello world!'
False
>>> 'car' not in 'Hello world!'
True
>>> 'house' not in 'Hello world!'
True
>>>

Subscribe to our Newsletter

Like us on Facebook

Share on Twitter

top
Developed by Signum Soft