Search code examples
pythonpython-3.xstringfindsubstring

Multiple substrings in string python within variable check


I have the following code:

check = "red" in string_to_explore

How can I replicate it for multiple substrings?

I tried with:

check = "red|blue" in string_to_explore

but doesn't seem to work.

Thanks in advance,


Solution

  • Python str methods doesn't accept regex pattern, for that use re module:

    import re
    
    text = "foo foo blue foo"
    pattern = re.compile(r"red|blue")
    check = bool(pattern.search(text))
    print(check) # True