SyntaxError: invalid decimal literal in Python (Solved)

Updated Sep 22, 2023 ⤳ 3 min read

The Python error “SyntaxError: invalid decimal literal” occurs if you use an invalid decimal literal in a Python expression, like 453a, or amount = 12.5a, or 100_step = 'someValue'

Here’s what the error looks like:


File /dwd/sandbox/test.py, line 1
  100_step = 'someValue'
    ^
SyntaxError: invalid decimal literal

A SyntaxError is a type of error that occurs when the Python's interpreter encounters a line of code that does not conform to the rules of the Python syntax.

What's a literal? You may ask. 

🎧 Debugging Jam

Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! 🎵💻🚀

24/7 lofi music radio banner, showing a young man working at his computer on a rainy autmn night with hot drink on the desk.

The term "literal" refers to an actual value in the source code. All the following values are literals:

  • 'someValue'
  • 12.5
  • 23
  • True

And here's a function call with a decimal literal as an argument:


# Calling a function with a numeric argument
addPrice(45.45)

How to fix "SyntaxError: invalid decimal literal"

The error "SyntaxError: invalid decimal literal" happens under the following scenarios:

  1. A decimal value containing letters
  2. An identifier (variable or function name) starting with a number

A decimal value containing letters: In Python, any literal starting with numbers is considered a decimal literal - no matter what side of an operator it is.

Based on Python's syntax, a decimal literal can't contain alphabet letters. For instance, 12.45a, 45p12, 13AM, and 45KG are invalid decimal literals.

Here are some examples that lead to "SyntaxError: invalid decimal literal":


# 🚫 Invalid decimal literals

int(45KG)
hour = 9h45
price = 45.99€
score = 34sc

To fix the issue, remove the letters in your numeric values.

An identifier (variable or function name) starting with a number: Identifiers (also referred to as names) are arbitrary names you use for definitions in the source code, such as variable names, function names, class names, etc.

Python identifiers are based on the Unicode standard annex UAX-31, which describes the specifications for using Unicode in identifiers.

That said, an identifier shouldn't begin with a number because Python's interpreter wouldn't recognize it as such. When a supposed-to-be identifier starts with numbers, Python considers it a decimal literal. That's why it raises a "SyntaxError: invalid decimal literal" once it detects a letter in it:


# 🚫 Raises SyntaxError: invalid decimal literal 
100_steps = range(0, 100)

# 🚫 Raises SyntaxError: invalid decimal literal 
def 100_words:  
    # some code here ...
    pass

In the above example, since the variable name starts with a number, it's considered a decimal literal. But once Python encounters the word 'step', it throws the error. Please note _ is a valid token used as a delimiter in numbers (Python +3.6)

You can correct your identifiers like so:


# ✅ variable names start with letters & underscores. 
hundred_steps = range(0, 100)

# ✅ function names start with letters & underscores. 
def hundred_words():  
    # some code here ...
    pass

Alright, I think it does it! I hope this short guide helped you fix your problem.

Thanks for reading.

Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.

`