The python error ValueError: Invalid literal for long() with base 10: occurs when the built-in long() function is called with a string argument which cannot be parsed as a long. The int() function returns a long object created from a string or number. If there are no arguments, it returns 0. If the string or number can not convert as a long, the error ValueError: Invalid literal for long() with base 10: will be thrown.
The long() function converts the given string or number to an integer. The default base for the long() buit-in function is 10. The digits are supposed to be between 0 and 9. The long can also have negative numbers. If the string is empty or contains a value other than a long number, or if the string contains a float, the error ValueError: Invalid literal for long() with base 10: will be thrown.
The long() function converts the string to a long if the string is a valid representation of the long and validates against the base value if specified (default is base 10). The long() build in function displays the error message that shows you the exact string you were trying to parse as a long.
Exception
The error ValueError: Invalid literal for long() with base 10: will be shown as below the stack trace. The stack trace shows the line that the long() build in function fails to parse to convert a long from a string or a number.
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print long(x)
ValueError: invalid literal for long() with base 10: ''
[Finished in 0.1s with exit code 1]
How to reproduce this error
If the build in long() function is called with a string argument that contains an empty string, or contains a value other than a long, 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 long() function. The error ValueError: Invalid literal for long() with base 10: will be thrown as an empty string that can not be converted to a long.
x=''
print long(x)
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print long(x)
ValueError: invalid literal for long() with base 10: ''
[Finished in 0.1s with exit code 1]
Root Cause
If the build in long() function is called with a string argument that contains an empty string, or contains a value other than a long, or contains a float value, the long() function parses the string value to a long value as per the base if specified. These arguments can not be parsed into a long value since the string does not have a valid long value.
Valid arguments in long() function
The following are valid arguments for the built in function long(). If you use one of the below, there will not be any error.
long() function with no argument – The default long() function which has no argument passed returns default value 0.
print long() # returns 0
long() function with an integer value – If a long value is passed as an argument in long() function, returns the long value.
print long(5) # returns 5
long() functions with a string containing a long value – If a string having a long value is passed as an argument in long() function, returns the long value.
print long('5') # returns 5
long() function with a float value – If a float value is passed as an argument in long() function, returns the long part of the value.
print long(5.4) # returns 5
long() function with a boolean value – If a boolean value is passed as an argument in long() function, returns the long value for the boolean value.
print long(True) # returns 1
Invalid arguments in long() function
Below is some of the examples that will cause the error.
long() function with an empty string – The empty string can not be parsed as a long value
print long('') # throws ValueError: invalid literal for long() with base 10: ''
long() function with a string having a float value – If a string having a float value is passed as an argument, long() function will throw value error.
print long('5.4') # throws ValueError: invalid literal for long() with base 10: '5.4'
long() function with a non-long string – If a string contains a non-long values such as characters and passed as an argument, the long() function will throw value error.
print long('q') # throws ValueError: invalid literal for long() with base 10: 'q'
Solution 1
If the string argument contains a float value in the built in function long(), it will throw the error. The string argument should be converted as a float value before it is passed as an argument to the long() function. This will resolve the error.
Program
x='5.4'
print long(x)
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 3, in <module>
print long(x)
ValueError: invalid literal for long() with base 10: '5.4'
[Finished in 0.1s with exit code 1]
Solution
x='5.4'
print long(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 long number. It can be passed to the built in function long() as an argument. Otherwise, an error message would be shown to the user.
x='5.4'
if x.isdigit():
print "Long value is " , long(x)
else :
print "Not a long number"
Output
Not a long number
[Finished in 0.0s]
Solution 3
If a string occasionally contains a non-long number, the build-in function isdigit() is not a good choice. In this case, try-except will be used to solve this problem. If a long 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 ("Long value is " , long(x))
except:
print "Not a long number"
Output
Not a long number
[Finished in 0.0s]
Solution 4
If a string argument contains an empty string, the built-in function long() will throw an error. The empty string should be validated before it is passed to the long() function.
x=''
if len(x) == 0:
print "empty string"
else :
print long(x)
Output
empty string
[Finished in 0.0s]
Solution 5
If the string argument contains a float value in the built in function long(), 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 long() function. This will resolve the error.
x='2.3'
y = eval(x)
print type(y)
print long(x)
Output
<type 'float'>
2.3
[Finished in 0.1s]