← Go Back

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

The int() built-in function raises the ValueError exception when a string that cannot be converted to an integer is given. For example:

>>> int("30")      # Successful conversion.
30
>>> int("hello") # Error: Cannot convert "hello" to an integer.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'hello'
>>> int("") # Error: empty string.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''

"base 10" means that Python is trying to convert to a number in the decimal system (this is the default behaviour), since the int() function also allows you to work with other number systems (binary, hexadecimal).

To fix this error, ensure that the given string is not empty and contains an integer (no spaces, periods, dashes, etc.). If you want to convert to a floating point number (e.g., 3.14), use the float() built-in.

strings integers conversion


🐍 You might also find interesting: