I'm wondering how Python does string comparison, more specifically how it determines the outcome when a less than (<
) or greater than (>
) operator is used.
For instance if I put print('abc' < 'bac')
I get True
. I understand that it compares corresponding characters in the string, however its unclear as to why there is more, for lack of a better term, "weight" placed on the fact that a is less than b (first position) in first string rather than the fact that a is less than b in the second string (second position).
Here is a sample code which compare two strings lexicographically.
for different inputs the outputs are-
A pure Python equivalent for string comparisons would be:
This function does the equivalent of the real method (Python 3.6 and Python 2.7) just a lot slower. Also note that the implementation isn't exactly "pythonic" and only works for
<
comparisons. It's just to illustrate how it works. I haven't checked if it works like Pythons comparison for combined unicode characters.A more general variant would be: