Python Forum
Single digits seem to be greater than double digits
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Single digits seem to be greater than double digits
#4
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:

>>> [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.
Reply


Messages In This Thread
RE: Single digits seem to be greater than double digits - by perfringo - Nov-20-2020, 10:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  If a set element has digits in the element tester_V 3 453 Mar-25-2024, 04:43 PM
Last Post: deanhystad
  method to remove zero digits from fraction in decimal Skaperen 17 3,211 Oct-23-2022, 04:02 AM
Last Post: Skaperen
  First line with digits before last line tester_V 5 1,591 Aug-22-2022, 09:04 PM
Last Post: tester_V
  Finding First Digits in String giddyhead 4 1,507 Aug-17-2022, 08:12 PM
Last Post: giddyhead
  Adding Decimals to classes with OOP + rounding to significant digits (ATM demo) Drone4four 7 2,510 May-04-2022, 06:15 AM
Last Post: Drone4four
  checking for valid hexadecimal digits Skaperen 3 6,744 Sep-02-2021, 07:22 AM
Last Post: buran
Photo How can I use 2 digits format for all the number? plumberpy 6 2,521 Aug-09-2021, 02:16 PM
Last Post: plumberpy
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 7,186 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  Digits of a number 1234 2 1,931 Nov-27-2020, 05:43 PM
Last Post: perfringo
  Printing digits after the decimal point one by one uberman321 1 1,857 Oct-20-2020, 08:10 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020