fuckingpython.lol

Python errors › ValueError

ValueError: invalid literal for int() with base 10

Updated

The Python error "invalid literal for int() with base 10" means int() got text that is not a whole number, such as '12.50', '' or '1,000'. The fix for each case.

What you see

main.py
price = "12.50"
print(int(price))
Python 3.15 prints
Traceback (most recent call last):
  File "/home/me/project/main.py", line 2, in <module>
    print(int(price))
          ~~~^^^^^^^
ValueError: invalid literal for int() with base 10: '12.50'

What it means

int() turns text into a whole number, and it accepts only digits, with an optional sign and spaces around them. "12.50" has a decimal point, so it is not an integer literal. The text in quotes at the end of the message is exactly what int() received.

"Base 10" is the default: ordinary decimal numbers. int("ff", 16) reads hexadecimal.

Why it happens

  • A decimal number, as in the example: "12.50", "3.0".
  • An empty string, the message then ends in ''. Pressing Enter at an input() prompt, or a blank cell in a CSV file, gives one.
  • Thousands separators or units: "1,000", "5 kg", "$20".
  • Text that is not a number at all, such as a header row read as data.

How to fix it

For a decimal, convert with float() first; int() then drops the fraction:

print(int(float("12.50")))   # 12

For money, keep it exact with decimal.Decimal:

from decimal import Decimal
print(Decimal("12.50"))

Strip separators before converting, and catch the error where input can be anything:

text = "1,000".replace(",", "")
try:
    n = int(text)
except ValueError:
    print("not a whole number:", repr(text))

How each Python version words it

PythonPrints
3.9–3.15ValueError: invalid literal for int() with base 10: '12.50'

Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.

The wording is the same in every version here.

Questions

Does int() accept spaces or underscores?

Spaces around the number are fine: int(" 42\n") is 42. Underscores between digits are allowed since Python 3.6, so int("1_000") is 1000.

Why does int(12.5) work but int("12.5") fail?

Given a float, int() truncates it. Given text, it parses it, and the text must be an integer.

Sources

Read on October 25, 2026.