+ 1

Why print("hey" < "hay") is false

18th Aug 2021, 7:35 AM
Shruti Munot
Shruti Munot - avatar
3 Respostas
+ 9
I must correct 🍇 Alex Tușinean 💜. The answer has nothing to do with comparison of key codes at all. Key codes are mapped to keyboard keys and are not the same meaning as character codes. This article has good detail if you want to learn more: https://docs.microsoft.com/en-us/windows/win32/learnwin32/keyboard-input When Python compares strings it compares ordinal values of the Unicode characters. You can see the ordinal values by using the ord() function: for i in "hey": print(ord(i)) 104 101 <-- 'e' 121 for i in "hay": print(ord(i)) 104 97 <-- 'a' 121 From left to right, none of the character values in "hey" are less than corresponding ones in "hay". It stops comparing at 'e' and 'a' because 101>97. So your string comparison "hey"<"hay" is false.
18th Aug 2021, 10:08 AM
Brian
Brian - avatar
+ 4
Check this. https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes because the key code of "e" is bigger than the key code of "a", your code returns false. edit: I realise you are searching for the python answer instead of js one but this is the one I could find the fastest, and the answer would be almost similar.
18th Aug 2021, 7:36 AM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
+ 2
So it means that as we go from a to z key code increases
18th Aug 2021, 7:40 AM
Shruti Munot
Shruti Munot - avatar