Search code examples
stringif-statementmembership

how to use if statement to test membership of a string


I'm having trouble understanding my logic in a simple Python statement. I'm trying to use 'not in' to test whether 's' or '5' is a part of the input but when I use either of them the same print statement is executed which says 's or 5 is not included'. Here is my code:

myinput = input('Enter input here')
if 's' or '5' not in myinput:
    print('s or 5 is not included')
else:
    print('s or 5 is included')

Could someone help me out? Thanks


Solution

  • As stated by Scott Hunter

    if ('s' not in myinput) and ('5' not in myinput):
        print('s or 5 is not included')
    else:
        print('s or 5 is included')
    

    De Morgan's law states that not (A or B) = not A and not B

    In your example, A = 's' is in myinput, and B = '5' is in myinput