Skip to main content

How to Solve "SyntaxError: invalid decimal literal" in Python

The SyntaxError: invalid decimal literal error in Python occurs when you attempt to use a variable name or a literal value that starts with a digit but is not a valid number.

This guide explains the rules for variable and literal naming in Python and demonstrates how to fix this common syntax error.

Understanding the Error: Python Naming Rules

Python has specific rules for naming variables, functions, classes, and other identifiers:

  • Valid Characters: Identifiers can contain letters (a-z, A-Z), digits (0-9), and underscores (_).
  • Starting Character: An identifier can not start with a digit. It must start with a letter (a-z, A-Z) or an underscore (_).
  • Case Sensitivity: Identifiers are case-sensitive (myVariable is different from myvariable).
  • Literals: Literals are notations for representing fixed values in source code. Literals can be numbers, strings, booleans, etc.

The SyntaxError: invalid decimal literal error occurs because Python is trying to interpret something as a number (a decimal literal), but it's not a valid number according to Python's rules.

Common Causes and Solutions

Variable Names Starting with a Digit

This is the most common cause:

# ⛔️ SyntaxError: invalid decimal literal
# 3_characters = ['a', 'b', 'c']

# ⛔️ SyntaxError: invalid decimal literal
# def 2_characters():
# return ['a', 'b']
  • You can not start variable name or function name with numbers.

Solution: Start variable and function names with a letter or underscore:

characters_3 = ['a', 'b', 'c']  # Valid variable name
_3_characters = ['a', 'b', 'c'] # Also valid (but less common)

def two_characters(): # Valid function name
return ['a', 'b']

def characters_2(): # Also valid
return ['a', 'b']
  • You can start the variable name with a letter, or an underscore.

Mixing Characters and Numbers Incorrectly in Literals

You can not mix letters and numbers in a numeric literal unless it's a valid float (with a decimal point) or a complex number (with a j suffix):

# ⛔️ SyntaxError: invalid decimal literal
# str(123abc)

# ⛔️ SyntaxError: invalid decimal literal
# age = 30yo

Solutions:

  • If it's a string: Enclose it in quotes:

    age = "30yo"  # Now it's a string
    another = "123abc" #This is now a string.
  • If it's a number: Remove the invalid characters, or use a different literal form.

    age = 30
    another = 123
  • Underscores for Readability (Valid): You can use underscores within numbers to improve readability (these are ignored by Python):

    large_number = 1_000_000  # This is a valid integer literal (1000000)
    print(large_number)

Conclusion

The SyntaxError: invalid decimal literal error is always caused by an improperly formatted identifier (variable, function, class name) or an invalid attempt to express a numeric literal.

  • By following Python's naming rules (starting identifiers with a letter or underscore, and not mixing letters/numbers in integer literals), you can easily avoid this error.
  • Use the type() function to make sure that the variable contains the expected type.
  • Always check for typos, and remember that the interpreter reads character by character, so small mistakes cause this error.