Python can only convert a valid numerical value to a floating point value. If you try to convert a value that contains a comma, spaces, or certain characters, you encounter an error that says “valueerror: could not convert string to float”.
In this guide, we talk about what this error means and why it is raised. We walk through an example of this error to help you see how to fix it in your code.
valueerror: could not convert string to float
Python offers a method called float() that converts a string to a floating-point number.
This method is useful if you need to perform a mathematical operation on a value. You cannot perform math on a string; you can perform math on a floating-point.
The float()
method only allows you to convert strings that appear like floats. This means that you cannot convert a value if:
- A value contains spaces
- A value contains a comma
- A value contains non-special characters (i.e. “inf” is a special character, but “fd” is not)
The “valueerror: could not convert string to float” error is raised if you fail to meet any one of the three above criteria. This is because Python cannot convert a value to a float unless that value appears in a particular way.
An Example Scenario
Here, we write a program that converts U.S. Dollars (USD) into Great British Pounds Sterling (GBP).
To start, ask a user to insert the dollar value they want to convert to pounds:
dollar_value = float(input("Enter the value you want to convert to GBP: "))
We convert the value a user inserts to a floating point number so we can perform a mathematical calculation using the value later on.
Next, we set the exchange rate for USD to GBP. At the moment, 1 USD is worth 0.76 GBP:
exchange_rate = 0.76
With these two values, we can calculate how much our USD value is worth in GBP. To do this, we multiply “dollar_value” by “exchange_rate”:
final_value = dollar_value * exchange_rate print("{} USD is equal to {} GBP".format(dollar_value, final_value))
We’ve used a .format() statement to create a message that informs the user of how much their USD is worth in GBP. Let’s run our code and see what happens:
Enter the value you want to convert to GBP: 23 23.0 USD is equal to 17.48 GBP
Our code runs successfully. Let’s see what happens if we try to insert a number into our code that is not formatted correctly:
Enter the value you want to convert to GBP: 2,300 Traceback (most recent call last): File "main.py", line 1, in <module> dollar_value = float(input("Enter the value you want to convert to GBP: ")) ValueError: could not convert string to float: '2,300'
Our code returns an error.
The Solution
Our code works when we insert a valid floating-point value into our program. We cannot rely on the fact that every user will do this.
When we’ve tried to insert a value in the thousands (2,300), we’ve added a comma. This has made our floating point number invalid.
To solve this problem, we can use a “try…except” block:
try: dollar_value = float(input("Enter the value you want to convert to GBP: ")) exchange_rate = 0.76 final_value = dollar_value * exchange_rate print("{} USD is equal to {} GBP".format(dollar_value, final_value)) except: print("Please insert a valid number. Currencies cannot contain commas, spaces, or characters.")
Our program will try to run the code in the “try” block. If our program is unsuccessful, the “except” block will be run.
Run our code and insert an invalid floating point value.
Our code returns:
Enter the value you want to convert to GBP: 2,300 Please insert a valid number. Currencies cannot contain commas, spaces, or characters.
Let’s execute our code with a valid value:
Enter the value you want to convert to GBP: 23 23.0 USD is equal to 17.48 GBP
In both cases, our code works successfully. In the first case, where we inserted an invalid value into our program, the contents of our “except” block executed. In the second case, our program calculated how much 23 USD is equal to in GBP.
Conclusion
The “valueerror: could not convert string to float” error is raised when you try to convert a string that is not formatted as a floating point number to a float.
You can solve this error by adding a handler that makes sure your code does not continue running if the user inserts an invalid value. Alternatively, you can add in additional input validation before converting a number to a float()
to make sure a number is valid.
Now you’re ready to fix this common Python error in your code.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.