Borislav Hadzhiev
Last updated: Apr 25, 2022
Photo from Unsplash
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.
Here is an example of how the error occurs.
# ⛔️ 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.
my_num = 8 print(my_num) # 👉️ 8
Alternatively, you can wrap the value in a string.
my_num = '08' print(my_num) # 👉️ '08'
The same applies to dictionary keys and list items.
# ⛔️ 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.
my_dict = {'04': 'hi'} print(my_dict['04']) # 👉️ 'hi'
If you meant to specify an octal literal, prefix it with 0o
.
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.