Integers with leading zeroes

Ben Finney ben+python at
Wed Jul 22 10:40:31 CEST 2015

Laura Creighton <lac at> writes:

> The biggest use I have for decimal numbers that begin with 0 is in
> credit card numbers, account numbers and the like where the first
> check you do is 'does this thing have the correct number of digits'.

The following are examples of types from the real world that people
think of, and casually discuss, as “numbers”.

* Postal code
* Credit card number
* Telephone number
* Car registration plate number
* Personal Identification Number (PIN)

You can no doubt come up with other examples.

Despite that they are represented in text with digits, and the authority
that generates them may even use some sequence of integers, the types
should not be treated as numbers.

They are not quantities. Performing arithmetic on them (adding two
together, dividing by two, etc.) makes no sense. Representing them in a
different number base (e.g. hexadecimal) utterly changes their meaning.

And, as you've found, a leading zero makes a difference to the value. To
a computer (ignoring the legacy of octal representation), that makes
nonsense of the idea these are numbers.

So these types are not numbers and should not be treated that way in the

These are, from the point of view of your computer program, opaque
tokens. They should be encoded not using a number type, but a type which
represents them as only a string of decimal digits.

Best if they're stored without spaces or punctuation; convert to and
from some format for human use if you must, but only at the input/output
borders of the program.

> So far, all the examples I've been able to find in my code -- which
> does this sort of stuff a lot -- is looking at string versions of
> decimal numbers, but I suspect there is old code out there in the
> wild which just used integers.

Maybe so, but I consider the latter kind of code to be a terrible
practice since it leads to assumptions in the code which are unfounded,
and which no human would ever make. Treat them the way a human actually
would: as an opaque tokem, a text (sequence of characters) that happen
to contain digits.

 \            “Simplicity is prerequisite for reliability.” —Edsger W. |
  `\                                                          Dijkstra |
_o__)                                                                  |
Ben Finney

More information about the Python-list mailing list