Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
error   1   22711
Solving python error - ValueError: invalid literal for int() with base 10


We can get this error when trying to convert a variable to an integer.


Some examples are:

Trying to convert a string to an integer.

rana@brahma:~$ python3
Python 3.5.2 (default, Oct  8 2019, 13:06:37) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(int("dawd"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'dawd'

Trying to convert a float string to an integer.

>>> print(int("110.0"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '110.0'
>>> 


Trying to convert an empty string to an integer.

>>> print(int(""))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>> 



What is int()

int() is the python's inbuilt function which converts the given number or string into an integer. Signature of this function is:

int(x=0)

The default parameter is 0 which is also the default return value if no value is passed.

>>> print(int())
0
>>> 


Also, the default base to convert to is base 10. 

int(x, base=10)

If base is specified, then int() function tries to convert the given parameter to an integer in the given base.

>>> print(int('110', base=2))
6
>>>


If a float is passed to int() function then it returns the truncated value.

>>> print(int(110.10))
110
>>> 



How to avoid this error?

Before trying to convert a suspicious variable to an integer, first, check the type of variable using isinstance method.

>>> isinstance(10, int)
True
>>> isinstance(10, float)
False
>>> isinstance(10.1, int)
False
>>> isinstance(10.1, float)
True
>>> 

Or simply use the try-except block to handle the error.

>>> try:
...     a = int('adwdaw')
... except:
...     print('error in converting the variable')
... 
error in converting the variable
>>> 

If you are trying to convert a float string to an integer, you need to first convert it to float and then to an integer.

>>> text = '101.10'
>>> int(text)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '101.10'
>>> 
>>> f = float(text)
>>> f
101.1
>>> int(f)
101
>>> 




error   1   22711
1 comment on 'Solving Python Error - Valueerror: Invalid Literal For Int() With Base 10'
Login to comment

Dachawar Prabhakar Vijaykumar March 30, 2020, 3:38 p.m.
super explainationstahns alot

Related Articles:
Solving python error - TypeError: 'NoneType' object is not iterable
In this article we are trying to understand what a NoneType object is and why we get python error - TypeError: 'NoneType' object is not iterable, Also we will try different ways to handle or avoid this error, python error NoneType object is not iterable, iterating over a None object safely in python...
Solving Python Error- KeyError: 'key_name'
Solving KeyError in python, How to handle KeyError in python dictionary, Safely accessing and deleting keys from python dictionary, try except Key error in Python...
Solving Python Error - UnboundLocalError: local variable 'x' referenced before assignment
UnboundLocalError: local variable 'x' referenced before assignment, solved UnboundLocalError in python, reason for UnboundLocalError in python, global vs nonlocal keyword in python, How to solve Python Error - UnboundLocalError: local variable 'x' referenced before assignment, nested scope in python, nonlocal vs global scope in python...
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap