SyntaxError: leading zeros in decimal integer literals are not permitted

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

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

The Python "SyntaxError: leading zeros in decimal integer literals are not permitted" occurs when we 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

leading zero in integer literal

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

# Remove the leading zeros from the integer

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

main.py
my_num = 8 print(my_num) # πŸ‘‰οΈ 8

remove leading zeros from integer

Removing the leading zero from the integer resolved the issue.

main.py
# ⛔️ Causes error my_num = 007 # -------------------------------- # βœ… Works as intended my_num = 7

Numbers cannot begin with leading zeros and converting a string that contains leading zeros to a number automatically drops the leading zeros.

main.py
my_str = '0007' my_int = int(my_str) print(my_int) # πŸ‘‰οΈ 7

numbers cannot begin with leading zeros

The only integer that can start with 0 in Python is 0 itself.

main.py
my_int = 0 print(my_int) # πŸ‘‰οΈ 0

# Wrapping the value in a string to solve the error

Alternatively, you can wrap the value in a string.

main.py
my_num = '08' print(my_num) # πŸ‘‰οΈ '08'

wrapping the value in a string

When the value is wrapped in quotes, it is a string and can start with leading zeros.

# Formatting a string with leading zeros

You can use a formatted string literal if you need to pad the value to a certain length with leading zeros.

main.py
my_int = 7 result = f'{my_int:03}' print(result) # πŸ‘‰οΈ 007 result = f'{my_int:04}' print(result) # πŸ‘‰οΈ 0007 result = f'{my_int:05}' print(result) # πŸ‘‰οΈ 00007

formatting string with leading zeros

If you need to format a list of integers to N leading zeros, use a list comprehension.

main.py
a_list = [3, 5, 7, 9] new_list = [f'{item:03}' for item in a_list] print(new_list) # πŸ‘‰οΈ ['003', '005', '007', '009']

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

main.py
var1 = 'bobby' var2 = 'hadz' result = f'{var1}{var2}' print(result) # πŸ‘‰οΈ bobbyhadz

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format specification mini-language in expression blocks.

The 0 after the colon is the pad character.

main.py
my_int = 7 result = f'{my_int:03}' print(result) # πŸ‘‰οΈ 007

The examples pad the string to a length of 3, 4 or 5 with leading zeros if necessary.

If the string is already of the specified length, no leading zeros are added.

# Wrapping dictionary keys or list elements in quotes

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'

Make sure to wrap all list elements in quotes if they start with leading zeros.

main.py
my_list = ['01', '02'] print(my_list[0]) # πŸ‘‰οΈ '01' print(my_list[1]) # πŸ‘‰οΈ '02'

# Creating octal literals

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

creating octal literals

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

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.