Skip to content
Advertisement

Why does (‘a’ > ‘b’ ) evaluates to false in Python? [duplicate]

I am not getting how does python evaluates this expression.What does it consider while evaluating this kind of expression?

The code snippet: print(‘a’>’b’)

Advertisement

Answer

If you compare the strings in python, it will consider the ascii values of the characters. Ascii value of ‘a’ is 97 and Ascii value of ‘b’ is 98. So basically you are asking if 97 > 98 which is false. That is the reason (‘a’ > ‘b’) evaluates to false.

Below are some examples

'a' > 'b' -> false
'ac' > 'ab' -> true (here first characters are equal. So it will compare 'c' and 'b')
'ac' > 'ba' -> false (here first characters are different. So it will just compare first characters)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement