Skip to content Skip to sidebar Skip to footer

How To Compare Two String In Python ? I Am Getting This Error

Code syntax: if( email_from1 != email_from2 & email_subject1 != email_subject2): Error: unsupported operand type(s) for &: 'str' and 'str'

Solution 1:

In python & is a bitwise operator. It appears you need the logical operator, which would be and. In addition in Python you do not need the () around the expresion for an if. But using tuples you can just do:

ifemail_from1,email_subject1!=email_from2,email_subject2:

Solution 2:

Python '&' doesn't work with strings. It's a Bitwise Operator. use 'and' instead: eg 1:

a = 'omi'
b ='not omi'
c ='omi'if a == c && b!=c:
    print"hello"

output:

  File "test.py", line 5ifa== c && b!=c:
               ^
SyntaxError: invalid syntax

eg2 :

a = 'omi'
b ='not omi'
c ='omi'if a == c and b!=c:
    print"hello"

output:

hello

could you try so :

if( email_from1 != email_from2 and email_subject1 != email_subject2):

Post a Comment for "How To Compare Two String In Python ? I Am Getting This Error"