0

I am doing a simple check in python to see if a substring is contained within a bigger string. My code looks like this:

def check_product_group(product: str) -> str:

    if "CORE" or "Core" in product:
        return "CORE"
    elif "CASUAL" or "Casual" in product:
        return "CASUAL"
    else:
        return ""

I am passing in the string "Casual_Attire" to the check_product_group function and it keeps returning "CORE" even though "Casual_Attire" does not contain "CORE" or "Core" in it. It should return "CASUAL." I'm confused as to why this is happening. Would appreciate if someone could explain why this is happening.

Thanks!

  • 2
    `if "CORE" or "Core" in product:` is always true. It is because short circuit evaluation, where `"CORE"` evalues to true. What you want is `if ('core' in product) or ('CORE' in product)` – Epsi95 Jul 15 '21 at 15:58
  • 1
    It's parsed as `if "CORE" or ("Core in product"):` – Barmar Jul 15 '21 at 16:00

0 Answers0