>>> "spam" < "bacon"
False
>>> "spam" < "SPAM"
False
>>> "spam" < "spamalot"
True
>>> "Spam" < "eggs"
True
How are equal length strings compared? Why is "Spam"
less than "eggs"
? What if the strings are not the same length?
Lexigraphically.
The first bytes are compared and if the ordinal value of the first is less than of the second, it's lesser. If it's more, it's greater. If they are the same, the next byte is tried. If it's all ties and one is longer, the shorter one is lesser.
>>> "a" < "zzz"
True
>>> "aaa" < "z"
True
>>> "b" < "a"
False
>>> "abc" < "abcd"
True
>>> "abcd" < "abce"
True
>>> "A" < "a"
True
>>> ord("A")
65
>>> ord("a")
97
Since A
comes before a
in ASCII table, so S
in Spam
is considered smaller than e
in eggs
.
>>> "A" < "a"
True
>>> "S" < "e"
True
>>> "S" < "eggs"
True
Note that, String length in not considered in comparison. Rather ordinal values for each byte are compared starting from the first byte, as rightly pointed out by @MikeGraham in comments below. And as soon as mismatch is found, the comparison stops, and comparison value is returned, as in the last example.
From the docs - Comparing Sequences and Other Types: -
The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.
Also further in the same paragraph: -
Lexicographical ordering for strings uses the ASCII ordering for individual characters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With