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

The python ValueError: Invalid literal for int() with base 10: error occurs when the built-in int() function is called with a string argument which cannot be parsed as an integer. The int() function returns an integer object created from a string or number. If there are no arguments, it returns 0. If the string or number can not convert as an integer, the error ValueError: Invalid literal for int() with base 10: will be thrown.

The int() function converts the given string or number to an integer. The default base for the int() buit-in function is 10. The digits are supposed to be between 0 and 9. The integer can also have negative numbers. If the string is empty or contains a value other than an integer, or if the string contains a float, the error ValueError: Invalid literal for int() with base 10: will be thrown.

The int() function converts the string to an integer if the string is a valid representation of the integer and validates against the base value if specified (default is base 10). The int() build in function displays the error message that shows you the exact string you were trying to parse as an integer.



Exception

The error ValueError: Invalid literal for int() with base 10: will be shown as below the stack trace. The stack trace shows the line that the int() build in function fails to parse to convert an integer from a string or a number.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 1, in <module>
    print int('')
ValueError: invalid literal for int() with base 10: ''
[Finished in 0.1s with exit code 1]


How to reproduce this error

If the build in int() function is called with a string argument that contains an empty string, or contains a value other than an integer, or contains a float value, this error can be reproduced. In the example below, an attempt is made to pass an empty string to the build in int() function. The error ValueError: Invalid literal for int() with base 10: will be thrown as an empty string that can not be converted to an integer.

x=''
print int(x)

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 1, in <module>
    print int('')
ValueError: invalid literal for int() with base 10: ''
[Finished in 0.1s with exit code 1]


Root Cause

If the build in int() function is called with a string argument that contains an empty string, or contains a value other than an integer, or contains a float value, the int() function parses the string value to an integer value as per the base if specified. These arguments can not be parsed into an integer value since the string does not have a valid integer value.



Valid arguments in int() function

The following are valid arguments for the built in function int(). If you use one of the below, there will not be any error.

int() function with no argument – The default int() function which has no argument passed returns default value 0.

print int()    # returns 0

int() function with an integer value – If an integer value is passed as an argument in int() function, returns the integer value.

print int(5)   # returns 5

int() functions with a string containing an integer value – If a string having an integer value is passed as an argument in int() function, returns the integer value.

print int('5')   # returns 5

int() function with a float value – If a float value is passed as an argument in int() function, returns the integer part of the value.

print int(5.4)   # returns 5

int() function with a boolean value – If a boolean value is passed as an argument in int() function, returns the integer value for the boolean value.

print int(True)   # returns 1


Invalid arguments in int() function

Below is some of the examples that will cause the error.

int() function with an empty string – The empty string can not be parsed as an integer value

print int('')     # throws ValueError: invalid literal for int() with base 10: ''

int() function with a string having a float value – If a string having a float value is passed as an argument, int() function will throw value error.

print int('5.4')     # throws ValueError: invalid literal for int() with base 10: '5.4'

int() function with a non-integer string – If a string contains a non-integer values such as characters and passed as an argument, the int() function will throw value error.

print int('q')     # throws ValueError: invalid literal for int() with base 10: 'q'


Solution 1

If the string argument contains a float value in the built in function int(), it will throw the error. The string argument should be converted as a float value before it is passed as an argument to the int() function. This will resolve the error.

Program

x='5.4'
print int(x)

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print int(x)
ValueError: invalid literal for int() with base 10: '5.4'
[Finished in 0.1s with exit code 1]

Solution

x='5.4'
print int(float(x))

Output

5
[Finished in 0.0s]


Solution 2

The string represents a number that should be verified using the buid-in function isdigit(). If the function isdigit() returns true, the string contains a valid integer number. It can be passed to the built in function int() as an argument. Otherwise, an error message would be shown to the user.

x='5.4'
if x.isdigit():
	print "Integer value is " , int(x)
else :
	print "Not an integer number"

Output

Not an integer number
[Finished in 0.0s]


Solution 3

If a string occasionally contains a non-integer number, the build-in function isdigit() is not a good choice. In this case, try-except will be used to solve this problem. If an integer is present in the string, it will be executed in the try block. Otherwise, an error message will be displayed to the user in the error block.

x='5.4'
try:
	print ("Integer value is " , int(x))
except:
	print "Not an integer number"

Output

Not an integer number
[Finished in 0.0s]


Solution 4

If a string argument contains an empty string, the built-in function int() will throw an error. The empty string should be validated before it is passed to the int() function.

x=''
if len(x) == 0:
	print "empty string"
else :
	print int(x)

Output

empty string
[Finished in 0.0s]


Solution 5

If the string argument contains a float value in the built in function int(), it will throw the error. The string argument should be verified using an built in function eval() before it is passed as an argument to the int() function. This will resolve the error.

x='2.3'
y = eval(x)
print type(y)
print eval(x)

Output

<type 'float'>
2.3
[Finished in 0.1s]



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *