Python is a special language which allows you to handle errors and exceptions very well. With thousands of known exceptions and ability to handle each of them, all the errors are easily removable from the code. Keeping this in mind, we’ll discuss about the invalid literal for int() error in python.
Invalid literal for int() with base 10 is caused when you try to convert an invalid object into an integer. In Python, the int() function accepts an object and then converts it to an integer provided that it is in a readable format. So, when you try to convert a string with no integers in it, it’ll throw an error. This error belongs to the ValueError category as the value of the parameter is invalid.
Cause of invalid literal for int() with base 10 error:
ValueError in Python occurs when we one passes an inappropriate argument type. Invalid literal for int() with base 10 error is caused by passing an incorrect argument to the int() function. A ValueError is raised when we pass any string representation other than that of int.
Let us understand it in detail!
ValueError: invalid literal for int() with base 10
This error message informs that there is an invalid literal for an integer in base 10. This error means that the value that we have passed cannot be converted.
Let us consider an example:
It may happen that we can think that while executing the above code, the decimal part,i.e, ‘.9’ will be truncated giving the output 1. However, this does not happen as the int( ) function uses the decimal number system as its base for conversion. This means that the default base for the conversion is 10. In the decimal number system, we have numbers from 0 to 9. Thus,
nt() with base = 10 can only convert a string representation of int and not floats or chars.
Let us see a few examples where this error can occur:
In this example the value “pythonpool” is a string value passed to the int() method which gives rise to the error.
As the value we have used here is float inside string, this gives rise to invalid literal for int() error.
We get error in this example as we have used list inside string.
The error invalid literal for int() arises because we have used tuple inside string.
Here, we have used dictionary inside string which gives rise to the error.
The error arises in this code as we have used the empty string in the int() method.
Resolution to the Error: invalid literal for int() with base 10:
Using float to avoid decimal numbers:
Here, we first converted the string representation into float using the float() method. We then used the int() method to convert it into an integer.
using try-catch: to resolve invalid literal for int() with base 10
try: x=int("12.1") except: print("Error in converting to string")
Error in converting to string
Here we have used the try-catch method to rid the invalid literal for int() with base 10 error. Basically, if the error occurs inside the try block, it is caught in the catch block, thus preventing the error.
x="12" if x.isdigit(): x=int(x) print(type(x))
<class ' int '>
In this method, we first make sure that the content inside the string is integer using the isdigit() method. As a result, the error does not occur.
x="12" if x.isnumeric(): x=int(x) print(type(x))
<class ' int '>
Isnumeric method of the string returns the boolean stating if the string is a number. If the string contains a number then we’ll convert it to int, else not.
With this, we come to an end to this article. This was an easy way to get rid of the value error in Python. If the above method does not work, then one must if the string and make sure that it does not contain any letter.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.