Python ValueError: invalid literal for int()
A plain-words explanation of this Python message: what it means, the usual causes, and the steps that fix it.
The messageValueError: invalid literal for int() with base 10: '12.5'
Make the error yourself
Run with Python 3.14.10. This is the smallest example we could find that shows it.
Python program (main.py)
amount = "12.5"
print(int(amount))Output (the real message)
Traceback (most recent call last):
File "main.py", line 2, in <module>
print(int(amount))
~~~^^^^^^^^
ValueError: invalid literal for int() with base 10: '12.5'Why it happens
int() turns text into a whole number, but it only accepts digits, with an optional sign. Text such as "12.5", "abc", "" (empty), "1,000" or " 5 kg" is refused. The message ends with the exact text that failed, in quotes, so you can see what the user or the file really contained, including stray spaces and empty strings.
How to fix it
- Look at the text in the message. Is it empty, does it have a decimal point, a comma or a unit?
- For decimals, convert with
float()first, thenint()if you need a whole number. Rememberint()cuts off the decimal part. - Clean the text before converting:
.strip()removes spaces,.replace(",", "")removes thousand separators. - For input from users, wrap the conversion in
tryandexcept ValueErrorand ask again.
The corrected version
Python program (main.py)
text = "12.5"
try:
value = int(float(text))
except ValueError:
value = None
print(value)Output (the corrected version)
12How to avoid it
Never trust text from a user or a file to be a number. Check or catch the conversion in one place and reuse it.
Questions
Why does int("12.5") fail while int(12.5) works?
The first is text, and text must look like a whole number. The second is already a number, and int cuts it to 12.
This is an independent guide. It is not connected with Python, Microsoft, Google, GitHub or the Git project. Product names belong to their owners. Examples were run on 13 November 2026 in a practice folder or a blank workbook; your version may word a message slightly differently.