The ValueError: could not convert string to float error occurs when you try to convert a string that does not contain a float number to a float. If the python string is not formatted as a floating point number, you could not convert string to float. The python ValueError: could not convert string to float will be thrown. The error is caused by a parsing error in the float() function with a string argument that cannot be parsed as a float number.

The float() function is used to convert a string with a float value to a float value. The float function parses the string and converts it as a float number. If an error occurs while converting a string to a float number, the python interpreter will throws this value error.

The string is supposed to have a valid float number. If the string is empty or does not contain a valid float number, the python interpreter can not convert it to a float number while parsing the string. In this case, the error “ValueError: could not convert string to float:” is thrown by the python interpreter.



Exception

The error “ValueError: could not convert string to float:” will be shown as below the stack trace.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 1, in <module>
    print float('')
ValueError: could not convert string to float: 
[Finished in 0.0s with exit code 1]


Root Cause

The function float() is used to convert a string to a float number. The string is supposed to have a valid float number. If the string is either empty or does not have a valid float number, the python interpreter will throw an error while parsing the string. If the valid float value is passed to the string, this issue will be resolved.



Valid arguments in float() function

The following are the valid arguments for float() function. If you use one of the following, the error will not be thrown.

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 int(5.4)   # returns 5.4

float() functions with a string containing an float value – If a string having an 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 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


Invalid arguments in float() function

float() function with an empty string – The empty string can not be parsed as an 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


Solution 1

The string value should be checked whether it is a number or not using buid-in isnumeric() function. If a string contains a number, it is passed as an argument in the float() function. Otherwise, the user will be shown an error message.

Program

x='a'
print float(x)

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    print float(x)
ValueError: could not convert string to float: a
[Finished in 0.0s with exit code 1]

Solution

x= u'a'
if x.isnumeric():
	print "float value is " , float(x)
else :
	print "Not a number"

Output

Not a number
[Finished in 0.1s]


Solution 2

If a string occasionally contains a non-numeric number, the build-in function isnumeric() is not a good option. In this case, try catch method will solve this issue. If the string contains an float number, it will be executed in the try block. Otherwise, an error message will be shown to the user in the error block.

Program

x='a'
print float(x)

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    print float(x)
ValueError: could not convert string to float: a
[Finished in 0.0s with exit code 1]

Solution

x= u'a'
try:
	print ("float value is " , float(x))
except:
	print "Not a number"

Output

Not a number
[Finished in 0.1s]



Leave a Reply