Skip to content

How to Resolve Python Error "SyntaxError: leading zeros in decimal integer literals are not permitted"

Fix the Python 3 SyntaxError for leading zeros in integer literals (e.g., 07) by removing zeros, using strings, or applying proper formatting for display.

Python has specific rules for writing integer literals (whole numbers directly in code). The SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers occurs when you try to define an integer starting with a zero (like 07 or 010), unless that number is zero itself (0). This syntax was used for octal (base-8) literals in Python 2 but is disallowed for decimal integers in Python 3 to avoid ambiguity.

This guide explains why this syntax is invalid and provides the correct ways to represent numbers or format strings with leading zeros.

Understanding the Error: Integer Literal Syntax

In Python (specifically version 3 and later), integer literals are written as sequences of digits without unnecessary leading characters.

  • Valid Decimal: 123, 0, -45
  • Invalid Decimal: 01, 007, -05 (leading zeros are not allowed for non-zero numbers)

Python reserves number literals starting with 0 followed by specific letters for other bases:

  • Octal (Base-8): Prefix 0o or 0O (zero followed by lowercase 'o' or uppercase 'O'). Example: 0o10 (which is 8 in decimal).
  • Hexadecimal (Base-16): Prefix 0x or 0X. Example: 0x10 (which is 16 in decimal).
  • Binary (Base-2): Prefix 0b or 0B. Example: 0b10 (which is 2 in decimal).

The SyntaxError arises because writing 07 looks like an attempt at an octal literal (due to the leading 0) but then uses digits (8 or 9) that are invalid in octal (which only uses digits 0-7). To prevent confusion between intended octal numbers and accidental leading zeros in decimals, Python 3 disallows leading zeros in decimal integer literals altogether (except for the number zero itself).

The Cause: Leading Zeros in Decimal Integers (Python 3)

The error occurs when you write a non-zero integer literal starting with one or more zeros without using the specific prefixes (0o, 0x, 0b) for other bases.

try:
    number = 07
except SyntaxError as e:
    print(e)

try:
    config_code = 001
except SyntaxError as e:
    print(e)

zero = 0
print(zero) # Output: 0

Solution 1: Remove the Leading Zeros

If you intend the number to be a standard decimal integer, simply remove the unnecessary leading zero(s).

number = 7
config_code = 1

print(f"Number: {number}")              # Output: Number: 7
print(f"Config Code: {config_code}")    # Output: Config Code: 1

string_ver = "007"
int_ver = int(string_ver)
print(f"Integer from '007': {int_ver}") # Output: Integer from '007': 7

Output:

Number: 7
Config Code: 1
Integer from '007': 7

Python correctly interprets 7 as the integer seven. The int() function also correctly parses strings like "007" into the integer 7. The syntax error only applies to literals written directly in the code.

Solution 2: Store as a String

If preserving the leading zeros is important (e.g., for codes, identifiers, display purposes), store the value as a string by enclosing it in quotes.

id_code = "007"
zip_code = "01234"

print(f"ID Code: {id_code} (Type: {type(id_code)})")

print(f"ZIP Code: {zip_code} (Type: {type(zip_code)})")

Output:

ID Code: 007 (Type: <class 'str'>)
ZIP Code: 01234 (Type: <class 'str'>)

Solution 3: Formatting Strings with Leading Zeros (Padding)

If you have an integer and need to display it with leading zeros (e.g., padding to a fixed width), use string formatting methods. Do not try to store the integer literal with leading zeros.

f-strings (Python 3.6+) provide a concise way to pad with zeros.

number = 42
width = 5

padded_string = f'{number:0{width}}' # Pad 'number' with leading '0's to width 'width'

print(f"Original: {number}")                         # Output: Original: 42
print(f"Padded to width {width}: '{padded_string}'") # Output: Padded to width 5: '00042'

padded_3 = f'{number:03}' # Pad to width 3
print(f"Padded to width 3: '{padded_3}'")            # Output: Padded to width 3: '042'

padded_5 = f'{number:05}' # Pad to width 5
print(f"Padded to width 5: '{padded_5}'")            # Output: Padded to width 5: '00042'

Output:

Original: 42
Padded to width 5: '00042'
Padded to width 3: '042'
Padded to width 5: '00042'

Using str.zfill()

The zfill() string method pads a string on the left with zeros. You need to convert the number to a string first.

number = 42
width = 5

padded_zfill = str(number).zfill(width)

print(f"Padded with zfill({width}): '{padded_zfill}'") 

Using str.format()

The older .format() method can also achieve zero-padding.

number = 42
width = 5

padded_format = "{:0{}}".format(number, width) # Pass width as argument

print(f"Padded with .format(): '{padded_format}'") 

Intended Octal Literals (0o Prefix)

If you did intend to write an octal (base-8) number, you must use the 0o (zero followed by lowercase 'o') or 0O prefix in Python 3.


value_octal = 0o10
print(f"Octal 0o10 is decimal: {value_octal}") # Output: Octal 0o10 is decimal: 8

value_octal_large = 0o77
print(f"Octal 0o77 is decimal: {value_octal_large}") # Output: Octal 0o77 is decimal: 63

Handling Leading Zeros in Data Structures (Lists/Dict Keys)

The syntax restriction applies anywhere you write an integer literal, including inside list definitions or as dictionary keys.

Examples of Error Scenarios:

my_list = [01, 02, 10]        # ⛔️ SyntaxError on 01
my_dict = {01: "A", 02: "B"}  # ⛔️ SyntaxError on 01

Solutions: Use strings or remove leading zeros

my_list_str = ["01", "02", "10"]
my_list_int = [1, 2, 10]

my_dict_str_keys = {"01": "A", "02": "B"}
my_dict_int_keys = {1: "A", 2: "B"}

print(my_list_str)      # Output: ['01', '02', '10']
print(my_list_int)      # Output: [1, 2, 10]
print(my_dict_str_keys) # Output: {'01': 'A', '02': 'B'}
print(my_dict_int_keys) # Output: {1: 'A', 2: 'B'}

Use strings if the leading zero is significant; use regular integers otherwise.

Conclusion

The SyntaxError: leading zeros in decimal integer literals are not permitted is a Python 3 syntax rule preventing ambiguity with the historical (Python 2) and current (0o) syntax for octal numbers.

To resolve this:

  1. If you intended a decimal integer, simply remove the leading zero(s) (e.g., change 07 to 7).
  2. If you need to preserve the leading zeros (e.g., for codes), store the value as a string (e.g., "007").
  3. If you need to display an integer with leading zeros (padding), store it as a regular integer and use string formatting (f-strings like f'{num:05}', str.zfill(), or str.format()) when creating the output string.
  4. If you intended an octal number, use the correct 0o prefix (e.g., 0o7).

Understanding this syntax rule helps write clean and unambiguous Python 3 code.

Navigation