Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing string for equality with == operator in python [closed]

I am trying to compare two string variables with == and it is not working for some reason. For example this code

print(dictionary[0])
print("A")
print(dictionary[0] == "A")

prints out

A    
A
False

I don't understand why it returns False when they are clearly equal.

like image 560
Branko Andrews Avatar asked Jul 04 '26 17:07

Branko Andrews


1 Answers

it works on me

dictionary = {0:"A"}
print(dictionary[0])
print("A")
print(dictionary[0] == "A")


result: 
A
A
True

possible reason is the length, maybe it contain space try to use strip() to remove the space or check the length of a string len(dictionary[0])

print(dictionary[0].strip() == "A")
print len( dictionary[0] )
like image 179
Randy Arguelles Avatar answered Jul 07 '26 08:07

Randy Arguelles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!