How to fix "ValueError: invalid literal for int() with base 10" in Python
Last Updated :
28 Apr, 2025
This error occurs when you attempt to convert a string to an integer using the int() function, but the string is not in a valid numeric format. It can happen if the string contains non-numeric characters, spaces, symbols. For example, converting a string like "abc" or "123.45" to an integer will trigger this error, as Python expects a valid integer string (e.g., "123") without any extraneous characters or formatting.
Causes of the ValueError: invalid literal for int() with base 10
There are several reasons why this error occurs:
- Non-numeric characters in the input string: The input string contains characters that are not digits, such as alphabetic characters or special symbols.
- Incorrectly formatted strings: The string might have punctuation marks, spaces, or characters that are not allowed in an integer representation.
- Leading or trailing spaces: If the string contains spaces before or after the number, it can lead to an error.
- Punctuation marks: Periods, commas or other special characters in the string will cause the error.
Examples of common scenarios leading to this error
Example 1: In this example, we try to convert the string "abc123" to an integer using int(), but it contains letters, making it an invalid base-10 number and causing a ValueError.
Python
Output
Hangup (SIGHUP)
Traceback (most recent call last):
File "/home/guest/sandbox/Solution.py", line 1, in <module>
a = int("abc123")
ValueError: invalid literal for int() with base 10: 'abc123'
Example 2: In this example, we try to convert the string " 4 2 " to an integer using int(), but it contains extra spaces within the number, making it an invalid base-10 format and resulting in a ValueError.
Python
Output
Hangup (SIGHUP)
Traceback (most recent call last):
File "/home/guest/sandbox/Solution.py", line 1, in <module>
a = int(" 4 2 ")
ValueError: invalid literal for int() with base 10: ' 4 2 '
Example 3: In this example, we try to convert "19.3" to an integer using int(), but the period (.) indicates a float, causing a ValueError since int() expects a whole number like "19".
Python
Output
Hangup (SIGHUP)
Traceback (most recent call last):
File "/home/guest/sandbox/Solution.py", line 2, in <module>
b = int(a)
ValueError: invalid literal for int() with base 10: '19.3'
Solutions to Fix "ValueError: invalid literal for int() with base 10"
Here are several ways to resolve this error:
1. Check and Clean Input Strings: Before converting a string to an integer, ensure that the string contains only valid numeric characters. You can remove any unwanted characters or spaces before conversion.
Python
a = int("123")
b = int(" 42 ".strip())
print(b)
Explanation:
- int("123") converts the string "123" directly to the integer 123.
- int(" 42 ".strip()) removes leading spaces from " 42 " using strip() and converts the resulting string "42" to the integer 42.
2. Stripping Whitespace: Leading and trailing spaces are common causes of this error. You can use the strip() method to remove the spaces before attempting conversion.
Python
a = " 123 "
b = int(a.strip())
print(b)
Explanation: int(a.strip()) removes the spaces, resulting in the string "123", which is then converted to the integer 123.
3. Validate Input Before Conversion: Use methods like isdigit() to check whether the string consists only of numeric characters before attempting to convert it. This can help avoid errors by ensuring the string is in a valid format.
Python
a = "123"
if a.isdigit():
b = int(a)
print(b)
else:
print("Invalid")
Explanation: isdigit() method checks if a string contains only digits. Since "123" is valid, it's safely converted to an integer. If not, the else block prints "Invalid", avoiding a ValueError.
Alternative approach
1. Using try-except blocks: When you're unsure about the format of the input string, using a try-except block can help handle the error without crashing the program.
Python
try:
value = int("abc123")
except ValueError:
print("Invalid")
Explanation: try-except block attempts to convert "abc123" to an integer. Since it contains letters, int() raises a ValueError, which is caught and "Invalid" is printed to prevent a crash.
2. Check for floating-point numbers: If the input might contain floating-point numbers (e.g., "19.3"), you can use float() for conversion, followed by rounding or handling decimals as needed.
Python
a = "19.3"
b = float(a)
print(b)
Explanation: string "19.3" is converted to a float using float(a). Since it's a valid decimal number, the conversion succeeds and 19.3 is printed.