Reputation: 699
I'm trying to compare two strings in python and for some reason they just aren't matching.
I've made sure there's no whitespace and they're both string types, but still no luck, even when they're exactly the same.
if versions['ksp_version'] is self.version:
The strings are both '1.0.2' and I've tried both is
and ==
with no luck.
Is there a better way to compare strings in python that I'm just missing?
EDIT: Some more context.
So, I added these lines and printed out the variables.
print versions['ksp_version']
print type(versions['ksp_version'])
print "===="
print self.version
print type(self.version)
And here's what we got.
1.0.2
<type 'str'>
====
1.0.2
<type 'str'>
Based on that, they look the same. They both have been stripped of whitespace using .strip()
.
Upvotes: 0
Views: 2538
Reputation: 4812
Hard to say without seeing the actual strings. When you read from a file, be sure to strip
the line you're comparing to get rid of potential \n
s at the end:
with open("input.txt") as f:
for line in f:
print line == "asd"
print line.strip() == "asd"
prints
False
True
for input.txt
:
asd
is
will only yield True
for strings that are the same object, i.e. strings known for certain are initialized to the same value before runtime.
Upvotes: 3