TypeError vs ValueError in Python Explained

TypeError and ValueError are two common Python errors that beginners often mix up.

This page explains when Python raises TypeError and when it raises ValueError. It will help you tell them apart, read error messages more clearly, and choose the right fix.

Quick answer

try:
    number = int(user_input)
except TypeError:
    print("Wrong type was passed")
except ValueError:
    print("Right type function, but bad value")

Use TypeError when the kind of object is wrong.

Use ValueError when the object type is acceptable, but the actual value cannot be used.

What is the difference between TypeError and ValueError?

The simplest way to remember it is:

  • TypeError = wrong kind of data
  • ValueError = right kind of data, wrong value

In other words:

  • Python raises TypeError when an operation is used with an object of a type it does not support.
  • Python raises ValueError when the type is allowed, but the value itself is not valid for that operation.

This page focuses on comparing these two errors so you can identify the right problem faster.

When Python raises TypeError

Python raises TypeError when your code uses an object in a way that its type does not support.

Common situations:

  • Adding a string and an integer with +
  • Passing a non-iterable where Python expects something iterable
  • Using a string as a list index
  • Passing None where a real object is expected

Example: adding incompatible types

x = 5
y = "3"

print(x + y)

Output:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

This fails because 5 is an integer and "3" is a string. Python does not automatically combine them with +.

Example: using the wrong type as a list index

numbers = [10, 20, 30]
index = "1"

print(numbers[index])

Output:

TypeError: list indices must be integers or slices, not str

A list index must be an integer, not a string.

Typical fix for TypeError

The fix is usually one of these:

  • Convert the value to the correct type
  • Pass a different object
  • Check whether a variable is None
  • Make sure the function gets the type it expects

If you need more detail, see TypeError in Python: causes and fixes.

When Python raises ValueError

Python raises ValueError when the type is acceptable, but the specific value is not.

Common situations:

  • int("abc")
  • float("hello")
  • Unpacking the wrong number of values
  • Passing a value format that a function does not accept

Example: converting invalid text to int

text = "abc"
number = int(text)

Output:

ValueError: invalid literal for int() with base 10: 'abc'

This is a ValueError, not a TypeError, because int() can accept strings. The problem is that "abc" is not a valid integer string.

Example: unpacking the wrong number of values

a, b = [1, 2, 3]

Output:

ValueError: too many values to unpack (expected 2)

The list type is fine. The problem is that the number of values does not match what the code expects.

Typical fix for ValueError

The fix is usually one of these:

  • Clean or validate the input
  • Check the data before converting it
  • Use try and except
  • Make sure the value format matches what the function expects

For a full guide, see ValueError in Python: causes and fixes.

Side-by-side examples

Looking at the two errors next to each other makes the difference easier to see.

TypeError example

print(5 + "3")

Output:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Why?

  • + does not support combining int and str directly.
  • The problem is the types.

Fix:

print(5 + int("3"))
print(str(5) + "3")

Output:

8
53

ValueError example

print(int("3.5"))

Output:

ValueError: invalid literal for int() with base 10: '3.5'

Why?

  • int() accepts strings
  • But "3.5" is not a valid integer string
  • The problem is the value

Another example:

print(int("abc"))

Output:

ValueError: invalid literal for int() with base 10: 'abc'

This is why input handling often leads to ValueError. The type may be correct, but the text does not contain a usable value.

If you are working with input conversion, see how to convert a string to int in Python and Python int() function explained.

How to decide which one you have

When you see one of these errors, use this quick check:

  1. Read the exception name first.
  2. Ask: Is the object itself the wrong type?
  3. If not, ask: Is the type allowed, but the value invalid?
  4. Check the exact line that failed.
  5. Inspect the variables involved.

A very useful beginner debugging trick is printing both the value and its type.

value = "123"

print(type(value))
print(value)

Output:

<class 'str'>
123

You can also use repr() to see the exact contents more clearly:

value = " 123 "
print(repr(value))

Output:

' 123 '

That helps you notice spaces, quotes, or unexpected characters.

Common fixes for TypeError

Here are some common ways to fix TypeError:

  • Convert values before combining them
  • Pass the expected type to functions
  • Check whether a variable is None before looping or indexing
  • Do not call lists or dictionaries like functions
  • Use isinstance() when you need to verify a type

Example: convert before combining

age = 25
message = "Age: " + str(age)

print(message)

Output:

Age: 25

Example: check for None

items = None

if items is not None:
    print(len(items))
else:
    print("items is None")

Output:

items is None

Example: verify a type

value = "42"

if isinstance(value, str):
    print(int(value))

Output:

42

If your error says something like unsupported operand type(s), this guide may help: fix "TypeError: unsupported operand type(s) for +" in Python.

Common fixes for ValueError

Here are common ways to fix ValueError:

  • Validate user input before conversion
  • Use try/except around int() and float()
  • Check list sizes before unpacking
  • Make sure the value exists before using methods that require a match
  • Remove extra spaces or unexpected characters

Example: safe integer conversion

user_input = "abc"

try:
    number = int(user_input)
    print(number)
except ValueError:
    print("Please enter a valid whole number")

Output:

Please enter a valid whole number

Example: clean input first

user_input = " 42 "
number = int(user_input.strip())

print(number)

Output:

42

Example: check unpacking size

parts = "red,green,blue".split(",")

if len(parts) == 3:
    a, b, c = parts
    print(a, b, c)
else:
    print("Expected exactly 3 values")

Output:

red green blue

If your error message mentions invalid literal for int(), see how to fix "ValueError: invalid literal for int() with base 10".

Beginner debugging checklist

When you are not sure whether the problem is TypeError or ValueError, go through this checklist:

  • Read the full traceback and find the failing line
  • Print both the value and its type
  • Look at the function you called and what it expects
  • Check whether the data shape matches what the code expects
  • If converting input, test with simple known-good values first

Useful debugging lines:

print(type(value))
print(repr(value))
print(type(a), repr(a), type(b), repr(b))
print(isinstance(value, str))
print(isinstance(value, int))

You can also inspect built-in functions:

help(int)
help(float)

That can help you understand what kinds of input those functions accept. For more background, see type conversion in Python and Python float() function explained.

Some errors look similar but mean something different:

  • AttributeError: the object does not have the attribute or method you tried to use
  • IndexError: a list position is outside the valid range
  • KeyError: a dictionary key does not exist
  • NameError: a variable name is not defined

These are different from type and value problems. If you want a broader overview, see Python errors and exceptions explained.

FAQ

Is int("123") a TypeError or ValueError if it fails?

It is usually a ValueError, because int() accepts strings, but the string value must look like a valid integer.

Why is adding a string and an integer a TypeError?

Because the + operation does not support those two types together without conversion.

Can the same line sometimes raise TypeError and sometimes ValueError?

Yes. It depends on the actual object passed at runtime. A function may reject one input because of type and another because of value.

Should I catch both TypeError and ValueError?

Only if both can realistically happen in that part of your code. Catch the most specific errors that fit the situation.

Which error is more common with user input?

ValueError is very common with user input because text often needs conversion and may contain invalid values.

See also