kamek
kamek

Reputation: 2440

String equality failure in Python. What gives?

Alright, I give up. I cannot understand the result I am getting from the following code (Python 2.6.6):

message.dest = message.dest.strip()
print type(message.dest)
print message.dest
if message.dest == 'UI':
    print "Equal!"
else:
    print "Not Equal!"

Somehow my output is:

<type 'str'>
UI
Not Equal!

Any ideas on what is going on here?

Upvotes: 16

Views: 5210

Answers (1)

animuson
animuson

Reputation: 54797

Originally posted by OP in body of question. Converted to community wiki answer.

I just wanted to follow-up with what the problem was in case anyone else finds themselves in a similar situation.

The problem was message.dest had an ASCII-encoded character in the string, e.g.,

>>> repr(message.dest)
"'\\x00UI'"
>>> print message.dest
UI

Personally my confusion stemmed mostly from the fact that the object type was <type 'str'>. This highlights the danger of using print statements as a debugging tool.

Upvotes: 6

Related Questions