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

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 –

For empty strings you will get the error. So, always check if a string is empty before passing it into int() or float() function.

    Tweet this to help others

Live Demo

Open Live Demo