-13
>>> '1.2.3'>'1.1.5'
True
>>> '1.1.3'>'1.1.5'
False
>>> '1.1.5'>'1.1.5'
False
>>> '1.1.7'>'1.1.5'
True
>>> '1.1.9'>'1.1.5'
True
>>> '1.1.10'>'1.1.5'
False
>>> '1.2'>'1.1.5'
True
>>> '1.2.9'>'1.1.5'
True
>>> '1.2.10'>'1.1.5'
True

Hi,

I am trying to compare two strings as shown above. First of all, I am surprised that python comparing strings of numbers. firstly I thought that it will just compare lengths, but for different values it's giving exact values and I am astonished. But, for '1.1.10' > '1.1.5' it's false... i don't know why.... can anyone help...

  • 3
    Strings are compared *lexicographically*, not numerically. `1` comes before `5` in the character set, just like `a` comes before `b`. It doesn't matter what the next character is unless there is a tie. – Martijn Pieters Aug 08 '16 at 10:34
  • 1
    I can't for the life of me imagine why you would have made any of the multiple incorrect assumptions you've described in this question. Why *shouldn't* a language be able to compare two values? Why would it just compare *lengths*? And what makes you think a string would be treated as a numeric value? – David Aug 08 '16 at 10:38
  • @David: other languages do, to a certain extent. JavaScript, Perl and PHP all treat strings consisting of digits as numeric in many contexts. With hilarious or even dangerous results. Granted, a string with multiple `.` in them won't be treated as such and you get the same results in any of those other languages as what Python does here, and JS wouldn't treat comparisons between two strings as numeric. – Martijn Pieters Aug 08 '16 at 10:39
  • @MartijnPieters: Sure, depending on a language's type system, that much is true and commonplace. But what numeric value would the OP expect these strings to have? For a string like `'10'` it would be intuitive, but for strings like these, these aren't numbers. I guess that's my point. – David Aug 08 '16 at 10:41
  • @MartijnPieters as you said, for '1.1.10'>'1.1.5', 10 comes after 5, it shouldn't be true? – Rahul Tejara Aug 08 '16 at 10:42
  • @RahulTejara: But there is no numeric value of `10` anywhere in that comparison. It doesn't matter that `10` comes after `5` because those aren't the values you're comparing. You're comparing *strings*. And `'5'` comes after `'1'`. – David Aug 08 '16 at 10:43
  • @RahulTejara: you are looking at it the wrong way. There are no *numbers* here, only *characters*. Python compares two strings character by character. Everything up to `'5'` and `'1'` is the same, and the character `'1'` (ASCII value 49) comes before `'5'` (ASCII value 54) in the characterset. – Martijn Pieters Aug 08 '16 at 10:45
  • @RahulTejara: Would `[49, 46, 49, 46, 53] < [49, 46, 49, 46, 49, 48]` be true? Of course not, because `53 > 49`. That's how strings are compared; byte by byte. – Martijn Pieters Aug 08 '16 at 10:46
  • Thank you... @MartijnPieters and @ David... Many downvotes on this question... please close or delete... – Rahul Tejara Aug 08 '16 at 10:47

1 Answers1

0
a = '1.1.10'
b = '1.1.5'
for a, b in zip(a, b):
    print(ord(a), ord(b), a > b)

49 49 False

46 46 False

49 49 False

46 46 False

49 53 False

vadim vaduxa
  • 221
  • 6
  • 16