Greater than operators

Hi all,

I’m studying for an admission test for an MSc in Data Science and it’s a test on basic Python knowledge. I’m stumped by this exercise

Exercise A
Say, for each of the following pairs of strings, whether s < t would be true or false, assuming that s had the value on the left and t had the value on the right:
“A” “9”
“Zurich” “acapulco”
“Abba” “ABBA”
“long_thing_with_a_$” “long_thing_with_a_&”
[email protected]” “King Kong”

So, the way I interpret it is that it should be “first item” < “second item”, and since the greater than goes like spaces > numbers > uppercase letters > lower case letters > symbols, I would expect it to be like this

True - "A" < "9" 
False - "Zurich" < "acapulco"
True - "Abba" < "ABBA"
True - "long_thing_with_a_$" < "long_thing_with_a_&"
False - "[email protected]" < "King Kong"

However, the output tells me it’s actually

What’s going on? I thought numbers were greater than uppercase letters and uppercase letters greater than lowercase

Thank you for your help, I appreciate you taking your time to read it

1 Like


Use ord() to inspect what is going on. When Python compares stuff like this, it converts them to numbers.
For example:

print(ord("A"), ord("9"))

65  57

65 is not less than 57. This is why the first answer is False

For combination of words or numbers:

zurich_ord = 0
acapulco_ord = 0

for letter in 'Zurich':
    zurich_ord += ord(letter)
for letter in "acapulco":
    acapulco_ord += ord(letter)
print(zurich_ord, acapulco_ord)

629 840

Therefore, the second answer is True

1 Like