Cannot Convert String to Float in Python

The “ValueError: could not convert string to float:<>” is an exception that is raised when we attempt to convert a string that cannot be converted into float using the float() function, for example, an empty string and a string with non-numeric characters.

In this article, we will see how to reproduce the error and how to fix it.

Reproduce “ValueError: could not convert string to float:<>”

In most cases, the error is raised in cases like the following:

Case 1: Attempting to convert an empty string to float.

Output:

ValueError: could not convert string to float: ''

Case 2: Trying to convert a string with non-numeric characters

Output:

ValueError: could not convert string to float: 'b2.67'

Output:

ValueError: could not convert string to float: '2,134'

There are, however, some exceptions to Case 2. Let’s look at some of them.

Exceptions to Case 2

Using non-numeric characters in a string does not always raise the “could not convert string to float:<>” error when the string is converted to float. Here are some examples of such cases.

Example 1: Using “_” within the numerical literal

Underscore, “_”, can be used to group digits to enhance readability. In this case, the underscore within the string to be converted may not raise an error. Examples,

Output:

2134.0
2134567.0
-45.9898

Example 2: Exponent floating point literals

When used correctly within a Python number, the letters “e” or “E” are interpreted as radix 10. For example,

Output:

1000.0
0.64
0.0
-90000.0

You can read more about the exceptions here: https://docs.python.org/3/reference/lexical_analysis.html#numbers

Solution to “ValueError: could not convert string to float:<>”

This section discusses three methods of fixing the error.

Method 1: Use a try-except statement to catch the error

In this method, we try to convert the string to float, and if that leads to ValueError, we do something else. In the following example, we set the result to 0.0.

Output:

could not convert string to float: 'b2.67'
0.0

Here is another example.

Output:

[0.0, 89.9, 0.0, 45.0, 0.0, -78.0, 8900000.0]

Method 2: Using re to remove unwanted characters in the string

This solution removes all unnecessary characters in the string using the re.sub() function by substituting non-numeric characters (\D) with an empty string (“”). Let’s see an example,

Output:

234678
<class 'str'>
234678.0

Another example

Output:

[345.0, 899.0, 657.0, 45.0, 65.0, 78.0, 8910.0]

Note: This method will fail in the cases discussed under the “Exceptions to Case 2” section. The pattern “\D+” will not understand the exceptions discussed unless we use a more robust pattern. In fact, “\D+”, will remove “+” and “-” for the cases used to define positive and negative numbers. See below.

Output:

[345.0, 89.0, 683.0, 45.0, 672.0, 78.0, 895.0]

As shown in the output, the regex pattern “\D+” failed to do what we expected. It removes +/-, decimal (.), and even the lexical character “e”.

The easiest way to solve this problem is to use a try-except statement and re simultaneously. Let’s see that.

Output:

[345.0, 89.9, 657.0, 45.0, 65.0, -78.0, 8900000.0, 567.0, 156.0]

Method 3: Using <str>.replace or <str>.strip

This solution mostly works when you know the nature of the strings you are trying to convert.

The <str>replace() method is used to replace unwanted characters anywhere in the string, whereas <str>.strip() removes the specific leading and trailing characters.

The latter assumes that the unwanted characters are contained on the string’s left or right side.

Here are some examples

Output:

678.0
27199.0
4.567

Conclusion

This method discussed ways of reproducing and solving “ValueError: could not convert string to float:<>”. The try-except method should be sufficient in most cases, but if you need to parse numbers from a string containing non-numeric, use methods 2 and 3.