Reputation: 110
I was having some problems with my code , a statement was giving true when it should've been false
For some reason '6' > '14' was true. I changed them into int s instead of str s and the problem was solved but i was wondering why this has happened in the first place
here's a picture !(http://prntscr.com/o1c7na)!
Upvotes: 1
Views: 66
Reputation: 42678
For comparing strings it compares char by char, the first char '6'
has a greater ASCII representation that '1'
hence it is bigger.
Here some examples of the behaviour:
>>> "a" > "b"
False
>>> "a" > "aaa"
False
>>> "1" > "2"
False
>>> "12" > "1"
True
>>> "6" > "14"
True
>>> "6" > "1"
True
The ASCII code can be retrieve with ord
:
>>> ord("6")
54
>>> ord("1")
49
Upvotes: 3
Reputation: 2477
This happens because the ascii string comparison happens with ascii code comparison of each letter one by one. So in the 1st step 6 is compared to 1 and since 6 is > 1 it returns true.
Upvotes: 0