How to Fix: SyntaxError: invalid decimal literal

3D illustration of a robot failing to paint a variable name starting with a number, representing the invalid decimal literal error.

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.

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:05

Similar Posts

Leave a Reply