Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does string comparison work on python? [duplicate]

Tags:

python

string

>>> "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?

like image 752
user1827019 Avatar asked Feb 18 '23 12:02

user1827019


2 Answers

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
like image 63
Mike Graham Avatar answered Feb 21 '23 00:02

Mike Graham


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

like image 25
Rohit Jain Avatar answered Feb 21 '23 00:02

Rohit Jain