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?
Python String Comparison operators In python language, we can compare two strings such as identify whether the two strings are equivalent to each other or not, or even which string is greater or smaller than each other.
Python comparison operators can be used to compare strings in Python. These operators are: equal to ( == ), not equal to ( != ), greater than ( > ), less than ( < ), less than or equal to ( <= ), and greater than or equal to ( >= ).
Python strings equality can be checked using == operator or __eq__() function. Python strings are case sensitive, so these equality check methods are also case sensitive.
The most straightforward method to determine if two strings are equal in Python is to use the == operator. This operator compares if two strings have the same value and returns True if they do. Else, it returns False .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With