Python throws the error, ValueError: invalid literal for int() with base 10: ”, when you pass a float string in int() function. Although, you can pass the string in float() function.
Consider this example –
>>> int('5555.00')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '5555.00'
The problem with this code is that you are passing a float string '5555.00' in int() function. The correct way of doing this is to first convert it into float() and then into int(). Check out this code for right implementation –
>>> int(float('5555.00'))
5555
According to Python, these are the acceptable inputs –
- passing a string representation of an integer into
int– Example –int('55') - passing a string representation of a float into
float– Example –float('55.00') - passing a string representation of an integer into
float– Example –float('55') - passing a float into
int– Example –int(55.00) - passing an integer into
float– Example –float(55)
For empty strings you will get the error. So, always check if a string is empty before passing it into int() or float() function.