Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Checking Membership in Python

    Python Programming Tutorials


    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:

    Symbol Operator Name Description
    in in The result of this operation becomes True if it finds a value in a specified sequence and False otherwise.
    not in not in The 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 inputs from the keyboard and checks whether the second string found in the first string or not.



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.