In Python, the ValueError: could not convert string to float
error occurs when you try to convert a string to a float, but the string cannot be converted to a valid float. In this tutorial, we will look at the common mistakes that could lead to this error and how to fix it with the help of examples.
Understanding the error
The float()
function in Python is used to convert a given value to a floating-point number. It takes a single argument which can be a number or a string. It is generally used to convert integers and strings (containing numerical values) to a floating-point number.
For a string to be successfully converted to a float in Python, it must contain a valid numerical value, which includes numerical values with a decimal point and/or a preceding sign (+ or -). If the string cannot be converted to a float value, it results in the ValueError: could not convert string to float
.
Common Scenarios for this error
The following are the common scenarios in which this error can occur when converting a string to float.
- The string contains non-numeric characters
- The string is empty
- The string contains multiple decimal points
- The string contains commas or other non-numeric characters
Let’s look at the above scenarios with the help of some examples.
# string contains non-numeric characters string_value = "abc123" float_value = float(string_value)
Output:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[5], line 3 1 # string contains non-numeric characters 2 string_value = "abc123" ----> 3 float_value = float(string_value) ValueError: could not convert string to float: 'abc123'
Here, the string string_value
contains alphabetical characters and thus it cannot be converted to a floating-point number. You’ll also get this error for other non-numerical characters, for example, if your string has spaces (preceding, trailing, or, in the middle). The only non-numerical characters allowed are – a decimal point, a sign character (+ or -) present at the beginning, or an exponent character.
# the string is empty string_value = "" float_value = float(string_value)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[6], line 3 1 # the string is empty 2 string_value = "" ----> 3 float_value = float(string_value) ValueError: could not convert string to float: ''
We get the same error with an empty string.
# string contains multiple decimal points string_value = "12.34.56" float_value = float(string_value)
Output:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[7], line 3 1 # string contains multiple decimal points 2 string_value = "12.34.56" ----> 3 float_value = float(string_value) ValueError: could not convert string to float: '12.34.56'
The string in the above example contains multiple decimal points and is not a valid numerical value and thus we get the error.
# string contains other non-numerical characters like comma, etc. string_value = "1,000.0" float_value = float(string_value)
Output:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[8], line 3 1 # string contains other non-numerical characters like comma, etc. 2 string_value = "1,000.0" ----> 3 float_value = float(string_value) ValueError: could not convert string to float: '1,000.0'
Although “1,000.0” can be considered as a valid numerical value but commas present in strings will cause this ValueError
when trying to convert it to a floating-point value.
Valid conversions
Let’s now also look at some cases where this error does not occur to understand what’s expected when converting a string to a float value.
# string is a valid integer string_value = "1234" float_value = float(string_value) print(float_value)
Output:
1234.0
In the above example, all the characters in the string are numerical characters; thus, we don’t get any errors converting it to a floating-point value.
# string is a valid real number string_value = "1234.75" float_value = float(string_value) print(float_value)
Output:
1234.75
We also don’t get an error for a string containing a valid real number with a decimal point.
# string contains a sign string_value = "+1234.75" float_value = float(string_value) print(float_value)
Output:
1234.75
Here, the string has a preceding sign and you can see that we don’t get an error. Note that if you use the sign incorrectly, you’ll get an error on converting it to a float value.
# string is in exponential notaion string_value = "1.23e-4" float_value = float(string_value) print(float_value)
Output:
0.000123
Exponential notation string value for a number is also valid when converting a string to a float.
How to Fix the Error?
To fix this error, make sure the string you are trying to convert contains a valid numerical value before converting it using the float()
function. There are many ways in which you can do so –
- Use a regular expression
- Use exception handling
Let’s look at both of these methods with the help of some examples.
Use a regular expression
Regular expressions (regex) are a sequence of characters that define a search pattern. They are used to match and manipulate text and are commonly used in programming languages and text editors.
You can use a regular expression to pattern match and extract valid numerical values from a string before applying the float()
function to convert them. This way you’ll only apply the float()
method to valid numerical strings and thus can avoid the ValueError: could not convert string to float
error.
To extract only numbers (optionally with a decimal point and a preceding + or – sign), you can use the following regular expression:
[-+]?[0-9]*.?[0-9]+
This regular expression matches:
[-+]?
: an optional + or – sign[0-9]*
: zero or more digits.?
: an optional decimal point[0-9]+
: one or more digits
This regular expression will match numbers such as 123
, -45.67
, +89.0
, and 0.123
.
Let’s look at an example.
import re # pattern to look for valid_num_pattern = r'[-+]?[0-9]*.?[0-9]+' # the string value string_value = "abc 123.45+234.12" # extract the numbers nums = re.findall(valid_num_pattern, string_value) # show the extracted numbers print(nums)
Output:
['123.45', '+234.12']
In the above example, we are using the findall()
function from the re
module to find all the matches for a valid number in a string, it returns a list of all the valid string numbers. You can now go ahead and apply the float()
function on each of the values in the returned list to convert them to floating-point values.
result = [float(val) for val in nums] print(result)
Output:
[123.45, 234.12]
You can see that we don’t get an error here.
Use exception handling
Alternatively, you can use error handling to detect this error and then perform corrective steps, for example, not converting the string to a float or maybe extracting the numeric values only from the string, etc.
Let’s look at an example.
string_value = "abc1234" try: float_value = float(string_value) except ValueError: print("Cannot convert string to float")
Output:
Cannot convert string to float
Here, we are catching the error and displaying a message that the string value could not be converted to a float value.
Conclusion
The ValueError: could not convert string to float
error occurs when you try to convert a string to a float, but the string cannot be converted to a valid float. To fix this error, you need to check the string for non-numeric characters, empty values, multiple decimal points, and commas or other non-numeric characters. You can use a regular expression to extract only valid numerical values from the string. You can also use error handling to avoid this error. By following the steps outlined in this tutorial, you can easily fix this error and continue working with numerical data in Python.
You might also be interested in –