Fix ValueError: could not convert string to float

In this post, we are going to learn how to fix ValueError: could not convert string to float, In Python converting a string object to the floating-point number can be done without any error. However when a string object contains an invalid float value with like commas, spaces or brackets we encounter “ValueError: could not convert string to float”.

1. What is ValueError: could not convert string to float


Whenever a string object contains a float point number, to perform a mathematical operation we have to convert it into float by using Python’s built-in function float(). However when the string object contains an invalid float number that contains the following:

  • string object contains an invalid float value like space, comma, special character, alphanumeric.
  • While converting an empty string to float.
  • While Convert a non-floating point string to a floating-point number.

Note: The string that contains NAN and INF can be converted to floating-point

In this example, we will demonstrate when ValueError: could not convert string to float occur in Python, In the below code, We are asking the user to input some floating value by using the Python built-in function input() it always takes input in string type that is ‘30,000’.While performing the mathematical operation on string float value, The “ValueError: could not convert string to float: ‘30,000” is encountered. The string that contains NAN and INF is converted to float without any error check first to print the statement.

#python3 how to fix ValueError: could not convert string to float
print(float('nan'))
print(float('inf'))


val = input("Please enter float value:")
print(type(val))


result = (float(val)*(6/12))
print("The output is  :", result)
print(float(''

Output

nan
inf

Please enter float value:30,000
<class 'str'>
ValueError: could not convert string to float: '30,000'

2. How to fix ValueError: could not convert string to float


We will demonstrate how we can fix the ValueError by using flowing two ways:

  • Using try-except block
  • Use valid string float point value

2.1 Using try-catch block


One solution is to use a try-except block to handle the exception and display a user-friendly message on the screen as used in the below code.

val = input("Please enter float value:")
print(type(val))

try:
 result = (float(val)*(6/12))
except: 
   print('The invalid string values to convert to a float') 

Output

Please enter float value:30,000
<class 'str'>
The invalid string values to convert to a float'

2.2 Use valid string float point value


We can avoid this error by inputting the valid string floating-point values. In the below example, the user has entered a valid string floating point value and performed some mathematical operation on it. Now the code will run without any error.

val = input("Please enter float value:")
print(type(val))


result = (float(val)*(6/12))
print("The output is  :", result)


Output

Please enter float value:30.369
<class 'str'>
The output is  : 15.1845

Summary

in this post, we are learnt how to Fix ValueError: could not convert string to float with example by using try-except blocks.