Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When I run the following statement, it returns false.

print('ant' < 'amoeba')

Why did it return false? What is the logic behind it? A link to an article about this will work too.

What I have tried:

I tried finding a reference to this in all of the python material I had, was unable to find about it on the internet as well.
Posted
Updated 25-Sep-22 22:47pm

Lexicographic comparison: see, for instance, this: How to compare two strings in Python[^].
 
Share this answer
 
When you do any comparison of two strings, the result of the operation is determined by the first difference encountered.

A pair of characters is examine from each pair of strings, starting with the "left most". If they are the same character, the comparison continues with the next pair. If they are different, the comparison of those characters alone determines the result of the whole comparison, so no further characters are even looked at. The result of the character to character comparison will depend on the "font value" - "UPPERCASE" characters are "smaller in value" than "lowercase" ones, so
'A' is less than 'a'.

This may help: https://alpharithms.s3.amazonaws.com/assets/img/ascii-chart/ascii-table-alpharithms-scaled.jpg[^] - it doesn't cover the whole character set (Unicode is enormous!) but it gives you enough to see what is going on.

This is particularly relevant if you compare dates as strings: "01-01-2022" is less than "31-12-1945" because the whole comparison resolved to "is the character '0' bigger than the character '3'?"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900