This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
In this article, you will learn how to solve ValueError: could not convert string to float.
Let’s look at code examples that produce the same error.
float('')
# Traceback (most recent call last):
# File "example.py", line 2, in <module>
# ValueError: could not convert string to float: ''
float('1,200')
# Traceback (most recent call last):
# File "example.py", line 6, in <module>
# ValueError: could not convert string to float: '1,200'
float('fd')
# Traceback (most recent call last):
# File "example.py", line 11, in <module>
# ValueError: could not convert string to float: 'fd'
How to Solve ValueError: could not convert string to float
In order to solve it, you can use the try...except
block.
a = 123
try:
float(a)
print("Input is a valid number")
except:
print("Input must be a valid number. Non-special characters, commas and spaces are not accepted.")
The post Solved – ValueError: could not convert string to float appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli

Ariessa Norramli | Sciencx (2021-03-07T11:02:37+00:00) Solved – ValueError: could not convert string to float. Retrieved from https://www.scien.cx/2021/03/07/solved-valueerror-could-not-convert-string-to-float/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.