Home › Errors › ValueError

Python ValueError: right type, impossible value

Exception: ValueErrorCategory: value validationRaised at: runtime

What this error means

The function received an argument of the correct type but with a value it cannot work with. int('abc') is the canonical case: a string is a perfectly legal argument type for int(), but these particular characters do not form a number. That type-vs-value distinction is exactly the line between TypeError and ValueError — and interviewers love asking for it.

The most-searched variant by far is ValueError: invalid literal for int() with base 10: 'abc'. The quoted part shows precisely what int() received, including invisible characters — read it before theorising.

A real example

age_text = input("Your age: ")   # user types: twenty five
age = int(age_text)
Traceback (most recent call last):
  File "app.py", line 2, in <module>
    age = int(age_text)
          ^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'twenty five'

What int() accepts is narrow: optional surrounding whitespace, an optional sign, then digits. Nothing else — not '12.5', not '', not '1,000', not '₹500'.

The other famous ValueErrors

How to debug it

  1. Read the rejected value in the message — repr-style quoting exposes empty strings, spaces, and stray symbols.
  2. Trace where that value came from: user input, a file, an API. The fix usually belongs at that boundary, not at the conversion.
  3. Clean deliberately: text.strip() for whitespace; a policy decision (not a reflex) for decimals — int(float(s)) truncates.
  4. Guard the conversion, narrowly: try: n = int(s) except ValueError: … — around one line, catching one type, with a real fallback plan.
  5. For unpacking errors, count both sides of the =; a starred target (first, *rest = items) absorbs variable length.

Fix it interactively

Practice more Python bugs →
🇮🇳 Hindi में समझें

Type सही था, value गलत — int("abc") में string देना allowed है पर "abc" number नहीं बनता। int() सिर्फ digits (और +/− sign) मानता है; "12.5" भी fail होगा। User input convert कर रहे हो तो सिर्फ conversion वाली line को try/except ValueError में रखो।

पूरी Hindi explanation पढ़ें →

Related errors

More conversion-adjacent failures in the full errors guide.