
This is a SyntaxError that confuses many beginners, especially if they are trying to format numbers neatly in their code. The message SyntaxError invalid decimal literal is a common error encountered when making a mistake in writing numbers in Python code.
It means: You tried to write a number that starts with a zero, but it isn’t a valid “octal” (base-8) number.
โก Quick Fix: SyntaxError: invalid decimal literal โ Python Leading Zero Fix with f-string :02d Zero-Padding for Clock, Date, and ID Formatting
Python rejected your number because it starts with 0 โ Python 3 treats a leading zero as the start of a non-decimal number literal (octal 0o, hex 0x, binary 0b) and crashes when the digits don’t match that format.
# WRONG โ leading zero on a decimal integer crashes immediately
week_1_sales = 150
week_2_sales = 90
week_3_sales = 05 # SyntaxError: invalid decimal literal
week_4_sales = 09 # SyntaxError: invalid decimal literal
# WRONG โ variable names starting with a number also fire this error
1st_place = "Alice" # SyntaxError: invalid decimal literal
# RIGHT โ remove the leading zero from the integer literal
week_3_sales = 5
week_4_sales = 9
# RIGHT โ use f-string zero-padding when the output needs leading zeros
hour = 9
minute = 5
print(f"Time: {hour:02}:{minute:02}") # Output: Time: 09:05
# RIGHT โ valid non-decimal literals use explicit prefixes
octal = 0o17 # octal โ prefix 0o
hex_val = 0xFF # hexadecimal โ prefix 0x
binary = 0b1010 # binary โ prefix 0bThe breakdown below covers the leading zero on integers, the variable-name-starting-with-a-digit trap, and the correct zero-padding patterns for display formatting.
The Cause
In Python (and many other languages), a number starting with 0 is not treated as a normal “decimal” (base-10) number.
0o(zero-oh) means the number is Octal (base-8).0x(zero-ex) means the number is Hexadecimal (base-16).0b(zero-bee) means the number is Binary (base-2).
Problem Code: You are probably trying to align your numbers, like this:
week_1_sales = 150 week_2_sales = 90 week_3_sales = 05 # CRASH!
Error: SyntaxError: invalid decimal literal
Python sees 05 and thinks you might be trying to write an octal number (because of the 0), but it’s not a valid format. In modern Python 3, this is just disallowed to prevent confusion.
The Fix: Just Remove the Leading Zero
Numbers don’t need leading zeros for alignment.
week_1_sales = 150 week_2_sales = 90 week_3_sales = 5 # Correct!
If you are trying to print a number with leading zeros (for a report or a clock), you must do it with an f-string.
Correct Way to Format:
hour = 9
minute = 5
# Use :02 to mean "pad with 0s to be 2 digits wide"
print(f"The time is {hour:02}:{minute:02}")
# Output: The time is 09:05SyntaxError: invalid decimal literal โ Leading Zeros, Number Bases, and Zero-Padding in Python 3
SyntaxError: invalid decimal literal fires before Python runs a single line. The parser sees a 0 at the start of a number token and checks for a valid base prefix โ 0o, 0x, or 0b. Finding raw digits instead, it stops immediately.
Two patterns trigger this error. Both have instant fixes.
A decimal integer with a leading zero. 05, 09, 007, 042 โ all crash in Python 3. Remove the leading zero. The value is identical: 5, 9, 7, 42. Python 2 allowed leading zeros as octal notation โ code ported from Python 2 triggers this error in Python 3 when a bare 0-prefixed number appears.
A variable name starting with a digit. 1st_place, 2nd_item, 3rd_score โ Python identifiers must start with a letter or underscore. A name starting with a digit crashes at parse time as an invalid literal. Rename to first_place, second_item, third_score, or place_1, item_2, score_3.
Zero-padding belongs in the output layer, never in the source code. Use f-string format specifiers for every case where leading zeros appear in display output.
Clock and time display
print(f”{hour:02}:{minute:02}:{second:02}”) # Output: 09:05:03
Fixed-width ID numbers
order_id = 7
print(f”Order #{order_id:05}”) # Output: Order #00007
Numeric alignment in reports
for i, val in enumerate([5, 42, 100], 1):
print(f”Week {i:02}: {val:>6}”) # right-aligned, zero-padded week number
The :0Nd format spec โ where N is the total width โ handles every zero-padding case. The value stays a plain integer in your code. The display layer handles the formatting. That separation keeps the SyntaxError permanently out of your source file.





