The key insight here is that string comparison doesn't compare based on alphabetical order or any natural order, but instead on the order of the characters in ASCII. You can see this order in an ASCII table.
Python will compare the first character in each string, and if it is the same will move on to the next. It will do this until the characters differ, or one string runs out (in which case the longer string will be considered greater).
As cdhowie pointed out, in a decimal ASCII encoding T
is 84, a
is 97, and t
is 116. Therefore:
>>> 'T' < 'a' < 't'
True
To show our second point:
>>> "apple" > "a"
True
To get a more natural comparison see: Does Python have a built in function for string natural sort?
To answer the question you added in an edit:
The simple answer is "yes". A conversion of 11.1
to '11.1'
is being performed.
The more complicated answer deals with how exactly comparison is implemented in python. Python objects can be compared if they implement the Comparison magic methods. There's a fair amount of reading you can do about python internals in that link.
As @glibdup pointed out, the above is incorrect. In python different types are compared based on the name of their type. So, since 'str' > 'float'
any string will be greater than any float. Alternatively, any tuple will be greater than any string.