ValueError: invalid literal for int() with base 10 (Fix)

This error happens when Python tries to convert a value to an integer with int(), but the value does not look like a valid whole number.

This page shows:

  • what the error means
  • common examples that cause it
  • why it happens
  • simple ways to fix it safely

If you want a broader explanation of exceptions, see Python errors and exceptions explained.

Quick fix

Use this when you expect a whole number from text input:

value = input("Enter a whole number: ").strip()

if value.lstrip("-").isdigit():
    number = int(value)
    print(number)
else:
    print("Please enter a valid whole number.")

This works well for:

  • numbers like "10"
  • negative numbers like "-3"
  • values with extra spaces around them

It does not accept decimal input like "3.14".

What this error means

Python raises this error when int() cannot turn a value into a whole number.

Valid integer strings look like:

  • "10"
  • "-3"
  • "0"

These are not valid for int() directly:

  • "3.14"
  • "hello"
  • "12a"

For example:

print(int("42"))

Output:

42

But this fails:

print(int("hello"))

Output:

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

If you need a full beginner guide to int(), see Python int() function explained.

Common example that causes the error

Here are three very common cases.

Example 1: letters in the string

value = "hello"
number = int(value)
print(number)

Output:

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

Example 2: decimal string

value = "3.14"
number = int(value)
print(number)

Output:

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

This fails because "3.14" is not an integer string.

Example 3: empty string

value = ""
number = int(value)
print(number)

Output:

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

An empty string is not a number.

Why it happens

This error usually happens for one of these reasons:

  • input() always returns a string
  • the string contains letters
  • the string contains a decimal point
  • the string is empty
  • the string contains commas or other formatting
  • the value has unexpected spaces or hidden characters

For example:

value = input("Enter your age: ")
print(type(value))
print(repr(value))
print(int(value))

If the user enters 12a, the conversion fails because "12a" is not a valid integer string.

If you are working with input from a user, this is a common problem. See how to convert user input to numbers in Python for more step-by-step examples.

How to fix it

There are several good ways to fix this, depending on your input.

Remove extra spaces

Use .strip() to remove spaces around the text:

value = "  25  "
number = int(value.strip())
print(number)

Output:

25

Validate before calling int()

If you only want whole numbers, check the string first:

value = "-12".strip()

if value.lstrip("-").isdigit():
    number = int(value)
    print(number)
else:
    print("Invalid whole number")

Output:

-12

Use try-except for safer input handling

This is often the easiest and safest pattern:

value = input("Enter a whole number: ").strip()

try:
    number = int(value)
    print("You entered:", number)
except ValueError:
    print("That is not a valid whole number.")

This is a good approach when input may be invalid.

Convert carefully when the input may not be clean

If your string may need cleaning first, fix the text before converting:

value = "1,000"
cleaned = value.replace(",", "")
number = int(cleaned)
print(number)

Output:

1000

For more examples, see how to convert string to int in Python.

Fix for decimal strings

int("3.14") fails because "3.14" is a decimal string, not a whole-number string.

Use float() if you need decimals

value = "3.14"
number = float(value)
print(number)

Output:

3.14

Convert to int only if you want to drop the decimal part

value = "3.14"
number = int(float(value))
print(number)

Output:

3

Be careful: this removes the decimal part. It does not round the number.

If you see a similar error with float(), read ValueError: could not convert string to float (Fix).

Fix for formatted numbers

Some strings look like numbers to humans but are not valid for int() directly.

Example:

value = "1,000"
number = int(value)
print(number)

This raises:

ValueError: invalid literal for int() with base 10: '1,000'

Remove commas first

value = "1,000"
cleaned = value.replace(",", "")
number = int(cleaned)
print(number)

Output:

1000

Be careful when cleaning input:

  • make sure the formatting really should be removed
  • do not remove characters blindly
  • confirm the cleaned value still means the same thing

Safer pattern for user input

A beginner-friendly pattern is:

  1. ask for input
  2. try to convert it
  3. show a clear message if it fails

Example:

while True:
    value = input("Enter a whole number: ").strip()

    try:
        number = int(value)
        print("Valid number:", number)
        break
    except ValueError:
        print("Please enter a valid whole number.")

This keeps asking until the user enters valid input.

Debugging checklist

If you are not sure why int() is failing, check the value before converting it.

Useful debugging commands:

print(value)
print(repr(value))
print(type(value))
print(value.strip())

What to look for:

  • spaces at the beginning or end
  • commas like "1,000"
  • empty strings like ""
  • decimal points like "4.5"
  • letters like "abc"
  • hidden characters such as "\n"

repr(value) is especially useful because it shows hidden characters clearly.

Common mistakes

These are common causes of this error:

  • calling int() on text like "abc"
  • trying int() on a decimal string like "4.5"
  • using int() on an empty string
  • reading user input that contains spaces or unexpected characters
  • trying to convert formatted numbers like "1,000" without cleaning them first

FAQ

Why does int("3.14") fail?

Because "3.14" is a decimal string, not an integer string. Use float() for decimals.

Does int() work with spaces?

It can handle some surrounding spaces, but using .strip() is safer for beginners.

How do I convert user input to an integer safely?

Use try-except or check the string first before calling int().

How do I convert "1,000" to an integer?

Remove the comma first, then call int():

value = "1,000"
number = int(value.replace(",", ""))
print(number)

See also