If string contains one of several substrings Python
if-statement, python, string
Solution
Use `any` for this:
>>> s = 'blahblahAA'
>>> any(x not in s for x in ('AA', 'BB', 'CC'))
True
Your current code is equivalent to:
if ('AA') or ('BB') or ('CC' not in string)
As `'AA'` is `True`(`bool('AA')` is `True`), so this always evaluates to `True`.
Problem
Similar to here: Does Python have a string contains substring method? This question only deals with one substring within a string, I want to test one of several. Something like: ``` if 'AA' or 'BB' or 'CC' not in string: print 'Nope' ``` However, if my test string is: ``` string='blahblahAA' ``` the if still evaluates to True and prints the statement. I'm probably just understanding the syntax incorrectly, any help would be appreciated. Thanks!