ProjectStack
python

ValueError: invalid literal for int() with base 10

You tried to convert a string to an integer using int(), but the string isn't a valid integer. It contains letters, a decimal point, spaces, or is empty.

Common causes

  • The string contains a decimal point: int('3.14') — use float() first or int(float('3.14'))
  • User input contains letters or extra whitespace: int('12px') or int('')
  • Reading from a file or API and the value isn't what you expected
  • An empty string or None was passed to int()

How to fix it

  1. Strip whitespace before converting: int(value.strip())
  2. For decimal strings, convert via float: int(float('3.14'))
  3. Validate the input first: if value.isdigit(): int(value)
  4. Use a try/except block to handle invalid values gracefully: try: int(value) except ValueError: ...

Example

Traceback (most recent call last): File "app.py", line 2, in <module> age = int(input_value) ValueError: invalid literal for int() with base 10: '25 years'

Calling int() on a string that contains text characters after the number

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.