Open In App

Invalid Decimal Literal in Python

Last Updated : 16 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A decimal literal is a number written with digits and an optional decimal point (.). It represents a floating-point value. For example, 20.25 is a valid decimal literal because it contains only digits and a single decimal point.

What Causes a "SyntaxError: Invalid Decimal Literal"

Python throws this error when a decimal literal includes characters that are allowed, such as letters or symbols. Let's look at some common causes:

1. Letters Inside a Decimal Literal

Python does not allow letters to appear within a numeric literal.

Example:

Python
f = 3.14a

Output:

SyntaxError: invalid decimal literal

Explanation: Even a single unexpected character like a causes Python to fail while interpreting the number.

2. Missing Operators Between Values and Letters

Python does not automatically separate numbers and letters. We might accidentally combine time-like values or other data without quotes.

Example:

Python
h = 9h45
print(h)

Output:

SyntaxError: invalid decimal literal

Explanation: If we're trying to represent time or a string like “9h45”, it should be enclosed in quotes.

3. Identifiers Starting with Numbers

In Python, variable names (identifiers) cannot begin with digits. If we try this, Python treats the number as a decimal literal and throws an error.

Example:

Python
12_v = 5.6

Output:

SyntaxError: invalid decimal literal

How to Fix "SyntaxError: Invalid Decimal Literal"

Below are some of the solutions to fix SyntaxError: Invalid Decimal Literal in Python:

Remove Letters from Decimal Literal

Ensure that the decimal literal consists only of numeric characters and a single decimal point. Remove any letters or other non-numeric characters.

Python
v = 3.14
print(v)

Output
3.14

Use Proper Identifiers

If the error is due to an identifier starting with a number, update the identifier name to comply with Python's naming conventions.

Python
v_1 = 5.6
print(v_1)

Output
5.6

Check for Typos or Unquoted Strings

Carefully review the code to identify and correct any typos that might be causing the error. Common mistakes include missing or misplaced decimal points.

Python
h = "9h45"
print(h)

Output
9h45

Next Article

Similar Reads