What do numbers starting with 0 mean in python?

In Python 2 (and a few more programming languages), these represent octal numbers.

In Python 3, 011 no longer works and you would use 0o11 instead.

In response to edit: and they are regular integers. They are just specified different way; and they are automatically converted by Python to an internal integer representation (which is base-2 actually, so both 9 and 011 are internally converted to 0b1001).


Both Python versions 2 & 3 understand octal written with leading '0o' and '0O' (Uppercase o), so be in the habit of using if when working with Python 2.x as well.

Only use leading zeros in numbers in strings.

You can convert integers from any of the other base systems with int().

>>> int(0o20)

16

If you want your output to display with leading zeros, then define it per this answer: Display number with leading zeros

If you ever plan to work with ZIP Codes, it's best to treat them as strings in all ways.


These are numbers represented in base 8 (octal numbers). Some examples:

Python 2 (old format)

Note: these forms only work on Python 2.x.

011 is equal to 1⋅8¹ + 1⋅8⁰ = 9,

0100 is equal to 1⋅8² + 0⋅8¹ + 0⋅8⁰ = 64,

027 is equal to 2⋅8¹ + 7⋅8⁰ = 16 + 7 = 23.

Python 3 (new format)

In Python 3, one must use 0o instead of just 0 to indicate an octal constant, e.g. 0o11 or 0o27, etc. Python 2.x versions >= 2.6 supports both the new and the old format.

0o11 is equal to 1⋅8¹ + 1⋅8⁰ = 9,

0o100 is equal to 1⋅8² + 0⋅8¹ + 0⋅8⁰ = 64,

0o27 is equal to 2⋅8¹ + 7⋅8⁰ = 16 + 7 = 23.