4

why python is giving output like this:

>>> 'apple' > 'T'
True
>>> 'apple' > 't'
False

It should be True for both cases.right?

Edit:

I got the Idea of ASCII Table.Thanks!!

Now what about this.Is 11.1 is being treated as '11.1'?

>>> 'apple' > 11.1
True
MBanerjee
  • 207
  • 1
  • 4
  • 10
  • Are you asking this because some data did not sort as you expected? If so, you can pass a key to the sort function so that case is normalized: `my_strings.sort(key=str.upper)`. – Steven Rumbalski Oct 17 '12 at 20:38
  • I edited my answer to include a brief answer to the question you edited in. If you have more questions about how that conversion works, I'd recommend reading the link I provided and starting a new SO question if you have one. – Wilduck Oct 17 '12 at 20:51

2 Answers2

8

Because a comes after T in the ASCII character set, but before t.

The decimal ASCII encoding of these letters:

  • T is 84.
  • a is 97.
  • t is 116.
cdhowie
  • 158,093
  • 24
  • 286
  • 300
6

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.

Community
  • 1
  • 1
Wilduck
  • 13,822
  • 10
  • 58
  • 90
  • In the linked question, the OP wanted numbers inside of a string to be interpreted as numbers, so that `'example22'` would sort after `'example7'`. I don't think that's relevant here. – Steven Rumbalski Oct 17 '12 at 20:34
  • 1
    @StevenRumbalski True enough. I guess my point in including that link was more to hint at a name for what the OP could be looking for. If this is actually a problem the OP needs to solve, and not just a question of academic curiosity, knowing the phrase "natural sort" might be a good start. Jeff Atwood has a pretty decent post about the problem (admittedly still dealing with numbers for the most part) http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html. – Wilduck Oct 17 '12 at 20:38
  • 2
    Fair enough. For most cases `my_list.sort(key=str.upper)` will probably be natural enough for expectations. – Steven Rumbalski Oct 17 '12 at 20:43
  • 1
    @StevenRumbalski and Wilduck: Its more like a question of academic curiosity,but knowing 'Natural Sort' and my_list.sort(key=str.upper) is added bonus :) – MBanerjee Oct 17 '12 at 20:47
  • 1
    The bit about comparing the string to the integer is incorrect. No conversion is taking place. See [here](http://stackoverflow.com/questions/3270680/how-does-python-compare-string-and-int). – glibdud Oct 19 '12 at 15:00
  • @glibdud Thanks! I knew something felt off about that answer. I'll update what I've written. – Wilduck Oct 19 '12 at 15:12