SyntaxError: invalid decimal literal in Python (Solved)

A hand holding the Python logo
Photo by, source: Pexels

🚫 SyntaxError: invalid decimal literal

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/, 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. 

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

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

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 ...

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 ...

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

Thanks for reading.

Author photo

Hey 👋 I'm a software engineer, an author, and an open-source contributor. I enjoy helping people (including myself) decode the complex side of technology. I share my findings on Twitter: @lavary_

If you read this far, you can tweet to the author to show them you care. Tweet a Thanks

In this article:

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