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

The built in float() function converts the given string or number to a decimal number. The decimal number is an integer number with fractions. The float number may even have negative numbers. If the string is empty or contains a value other than a decimal number, the error ValueError: invalid literal for float(): will be thrown.

The float() function converts the string to an integer if the string is a valid representation of the float number and validates against the decimal format. The float() build in function displays the error message that shows you the exact string you were trying to parse as a float number.



Exception

The error ValueError: invalid literal for float():  will be shown as below the stack trace. The stack trace shows the line that the float() build in function fails to parse to convert a float number from a string or a decimal number.

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


How to reproduce this error

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

x='1 0'
print float(x)

Output

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


Root Cause

If the build in float() function is called with a string argument that contains an empty string, or contains a value other than a float value, the float() function parses the string value to a float value. These arguments can not be parsed into a decimal value because the string does not have a valid float value.



Valid arguments in float() function

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

float() function with no argument – The default float() function which has no argument passed returns default value 0.0.

print float()    # returns 0.0

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

print float(5)   # returns 5.0

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

print float('5')   # returns 5.0

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

print float(5.4)   # returns 5.4

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

print float('5.4')   # returns 5.4

float() function with a fraction value – If a fraction value is passed as an argument in float() function, returns the float value.

print float('.4')   # returns 0.4

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

print float(True)   # returns 1.0

float() function with a scientific float value – If a scientific float value is passed as an argument in float() function, returns the float value.

print float(123E+2)  # returns 12300.0


Invalid arguments in float() function

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

float() function with an empty string – The empty string can not be parsed as a float value

print float('')     # throws ValueError: could not convert string to float: 

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

print float('q')     # throws ValueError: could not convert string to float: q

float() function with an invalid float string – If a string contains an invalid float values such as whitespace characters and passed as an argument, the float() function will throw value error.

print float('1 0')     # throws ValueError: invalid literal for float(): 1 0


Solution 1

If the string arguments contain two or more float numbers, the float() function can not parse decimal numbers. One float number is to be passed as an argument in the float() function. The string contains float number is parsed and converted to the float number. This will resolve the error.

x='12.34 15.45'
print float(x)

Exception

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

Solution

x='12.34'
print float(x)

Output

12.34
[Finished in 0.1s]


Solution 2

String arguments contain a float number, and a few unnecessary whitespaces inserted between numbers can cause an error. There should be no whitespace between numbers in a float number. One float number must be passed as an argument in the float() function without any white characters. The string contains float number is parsed and converted to the float number. This will resolve the error.

x='12.34 15'
print float(x)

Exception

ValueError: invalid literal for float(): 12.34 15

Solution

x='12.3415'
print float(x)

Output

12.3415
[Finished in 0.0s]


Solution 3

String arguments contain a float number, and the absence of a decimal point between the number and fractions can cause an error. There should be a decimal point between number and fraction. A valid float number must be passed as an argument in the float() function without any white characters. The string contains float number is parsed and converted to the float number. This will resolve the error.

x='12 34'
print float(x)

Exception

ValueError: invalid literal for float(): 12 34

Solution

x='12.34'
print float(x)

Output

12.34
[Finished in 0.1s]



Leave a Reply