If statement for strings in python?

Even once you fixed the mis-cased if and improper indentation in your code, it wouldn’t work as you probably expected. To check a string against a set of strings, use in. Here’s how you’d do it (and note that if is all lowercase and that the code within the if block is indented one level).

One approach:

if answer in ['y', 'Y', 'yes', 'Yes', 'YES']:
    print("this will do the calculation")

Another:

if answer.lower() in ['y', 'yes']:
    print("this will do the calculation")

Leave a Comment