jeudi 25 juin 2015

Combining multiple membership tests [duplicate]

This question already has an answer here:

To check if a string contains a substring, one can use "in" like so:

if "abc" in str:
    print("yay")

And to check if a string contains one of two substrings one can use "or" like so:

if "abc" in str or "def" in str:
    print("yay")

My question is whether or not python has a way to simplify that into something like this:

if "abc" or "def" in str:
    print("yay")

I know this won't work as intended because it will always evaluate to true. (Python will check for at least one of the two statements, either

  • "abc"
  • "def" in str

being true and "abc" will always evaluate to true)

Having said that, is there anyway to check for such a condition other than this, rather verbose, method:

if "abc" in str or "def" in str:
    print("yay")

Aucun commentaire:

Enregistrer un commentaire