SyntaxError: leading zeros in decimal integer literals are not permitted

avatar

Borislav Hadzhiev

Last updated: Apr 25, 2022

banner

Photo from Unsplash

SyntaxError: leading zeros in decimal integer literals are not permitted #

The Python "SyntaxError: leading zeros in decimal integer literals are not permitted" occurs when declare an integer with leading zeros. To solve the error, remove any leading zeros from the integer or wrap the value in a string.

syntaxerror leading zeros in decimal integer literals

Here is an example of how the error occurs.

main.py
# ⛔️ SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers my_num = 08

It's not allowed to have leading zeros in an integer in Python.

One way to solve the error is to remove any leading zeros.

main.py
my_num = 8 print(my_num) # 👉️ 8

Alternatively, you can wrap the value in a string.

main.py
my_num = '08' print(my_num) # 👉️ '08'

The same applies to dictionary keys and list items.

main.py
# ⛔️ SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers my_dict = {04: 'hi'} # ⛔ ️SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers my_list = [01, 02]

You can wrap the dictionary key in a string or drop the leading zero.

main.py
my_dict = {'04': 'hi'} print(my_dict['04']) # 👉️ 'hi'

If you meant to specify an octal literal, prefix it with 0o.

main.py
example_1 = 0o72 print(example_1) # 👉️ 58 example_2 = 0o73 print(example_2) # 👉️ 59

The octal notation has 8 rather than 10 as a base.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.