Checking Membership in Python

In Python, we can check whether a string or character is a member of another string or not using "in" or "not in" operators.

These two membership operators are described in the table below:

SymbolOperator NameDescription
ininThe result of this operation becomes True if it finds a value in a specified sequence and False otherwise.
not innot inThe result of this operation becomes True if it doesn't find a value in a specified sequence and False otherwise.

While comparing the string, these operators consider uppercase and lowercase letters or strings separately and make case sensitive comparisons.

A Python Program to Know Whether a String Exists in the Main String or Not

Example:
str1 = input('Please enter first string: ')
str2 = input('Please enter second string: ')
if str2 in str1:
    print(str2+' found in the first string.')
else:
    print(str2+' not found in the first string.')
Output:
Please enter first string: We are writing core python
Please enter second string: python
python found in the first string.

The above program takes two input from the keyboard and checks whether the second string found in the first string or not.


Scroll Back to Top