Leading 0 before integer is throwing error


In Python3, when tried to run "print(09)", it is showing below error:


File "<ipython-input-26-583eff382381>", line 7 print(09) ^ SyntaxError: invalid token


I googled and found that this leading causing octal represenation.. however I didn't get much info.

Isn't it uncommon behaviour of Python?


2 Answer(s)


Hi Shivakant,

In Python2:

A number with prefix 0 means octal representation. But it is no longer in use in Python3.

In Python3:

A number with prefix,

  • “0b” represents binary number -  e.g. 0b0011 = 3 in decimal representation
  • “0o” represents octal number -  e.g. 0o61 = 49 in decimal representation
  • “0x” represents hexadecimal number -  e.g. 0x12 = 18 in decimal representation

Bin_num = 0b0011

print("Binary Number:", "0b0011", "\n" "Decimal equivalent:", Bin_num, "\n")

Oct_num = 0o61

print("Octal Number:", "0o61", "\n" "Decimal equivalent:", Oct_num,"\n")

Hex_num = 0x12

print("Hexadecimal Number:", "0x12","\n" "Decimal equivalent:", Hex_num, "\n")

Reference documents:   

  • From Python2 library document:

        Integer arguments are parsed as follows:

        if the number starts with 0x, it is parsed as a hexadecimal number

        if the number starts with 0, it is parsed as an octal number

        if the number starts with 0b, it is parsed as a binary number

        otherwise, the number is parsed as a decimal number       

  • Updates in Python3:

        Ref doc: https://www.python.org/dev/peps/pep-3127/

        Refer "Removal of old octal syntax" Section in the document


Thanks Rajavel. it's useful information