How to fix "SyntaxError: invalid decimal literal" in Python
Tags: python, programming
Published: 2023-02-02 20:46:51

> ✋ **Update:** This post was originally published on my blog [decodingweb.dev](https://www.decodingweb.dev), where you can read the [latest version](https://www.decodingweb.dev/python-syntaxerror-invalid-decimal-literal-fix) for a 💯 user experience. _~reza_

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. 

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:

```python
# 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. <u>A decimal value containing letters</u>
2. <u>An identifier (variable or function name) starting with a number</u>

**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](https://www.decodingweb.dev/how-to-learn-a-programming-language#language-syntax-and-semantics), 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":

```python
# 🚫 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:

```python
# 🚫 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:

```python
# ✅ 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.

**❤️ You might like:**

- [How to fix "TypeError: ‘float’ object is not callable" in Python](https://www.decodingweb.dev/python-typeerror-float-object-is-not-callable-fix)
- [TypeError: 'int' object is not callable in Python (Fixed)](https://www.decodingweb.dev/python-typeerror-int-object-is-not-callable-fix)
- [TypeError: can only concatenate str (not “float”) to str (solutions)](https://www.decodingweb.dev/python-typeerror-can-only-concatenate-str-not-float-to-str-fix)
- [TypeError: can only concatenate str (not “int”) to str (Solutions)](https://www.decodingweb.dev/python-typeerror-can-only-concatenate-str-not-int-to-str-fixed)