Python – Unsupported operand type(s) for ‘str’ and ‘str’. Python

python

I've got the IF statement;

if contactstring == "['Practice Address Not Available']" | contactstring == "['']":

I'm not sure what is going wrong(possibly the " ' "s?) but I keep getting the error mentioned in the title.

I've looked in other questions for answers but all of them seem to be about using mathematical operates on strings which is not the case here. I know this question is kind of lazy but I've been coding all day and I'm exhausted, I just want to get this over with quickly.(Python newb)

Best Answer

| is a bitwise or operator in Python, and has precedence so that Python parses this as:

if contactstring == (""['Practice Address Not Available']"" | contactstring) == "['']":

Which generates the error you see.

It seems what you want is a logical or operator, which is spelled 'or' in Python:

if contactstring == ""['Practice Address Not Available']"" or contactstring == "['']":

Will do what you expect. However, since you're comparing the same variable against a range of values, this is even better:

 if contactstring in ("['Practice Address Not Available']", ['']):