16

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?

5
  • 8
    Try printing repr(message.dest). Commented Mar 7, 2012 at 15:39
  • 5
    would be nice if downvoters cared to explain themselves. Commented Mar 7, 2012 at 15:47
  • @Simon: see my answer, but in brief: No effort to resolve his own problem, this can only benefit OP, no-one else. Commented Mar 7, 2012 at 15:50
  • 2
    @Marcin, I have to disagree with your assessment. As edited this question serves as a valuable lesson in how to diagnose a string comparison problem. I don't see any need to reopen the question but it would be a shame if it were ever deleted. Commented Mar 7, 2012 at 16:57
  • @MarkRansom That was then, this is now. I have reopened the question. Commented Mar 7, 2012 at 17:37

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.