Comparing strings doesn't work like comparing numbers.
String comparison is based on character code points and can be desribed as follows:
- comparing the n-th characters of each string (starting with 0-th index) using the == operator
- if they’re equal, repeat this step with the next character
- in case of two unequal characters, string with the character that has the lower code point is 'less' than other
- if all characters are equal, the strings are equal
- if one string is shorter i.e. runs out of characters during comparison (one string is a “prefix” of the other), the shorter string is “less than” the longer one
Codepoints for characters 0..9 are:
String comparison is based on character code points and can be desribed as follows:
- comparing the n-th characters of each string (starting with 0-th index) using the == operator
- if they’re equal, repeat this step with the next character
- in case of two unequal characters, string with the character that has the lower code point is 'less' than other
- if all characters are equal, the strings are equal
- if one string is shorter i.e. runs out of characters during comparison (one string is a “prefix” of the other), the shorter string is “less than” the longer one
Codepoints for characters 0..9 are:
>>> [ord(str(i)) for i in range(10)] [48, 49, 50, 51, 52, 53, 54, 55, 56, 57]Therefore:
>>> '18' < '2' # code point of '1' is smaller than of '2' True >>> '18' < '02' # codepoint of '1' is larger than of '0' False
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.