How to Fix the Python Error: valueerror could not convert string to float

Python is known and loved for many of its most notable attributes. But one of the most famous is the language’s flexibility. You can manipulate data types and variables alike with an unprecedented level of ease when compared to many other programming languages. However, that flexibility can cause occasional issues for developers. And that’s often the case when you see a “valueerror could not convert string to float” Python error. However, the solution to this issue becomes readily apparent when we delve a little deeper into Python’s syntax and type structure.

An Overview of the Valueerror

The valueerror seems a little confusing in certain contexts. Especially if it comes up when you’re certain that you’re passing a correct value. However, this all comes back to Python’s inherent flexibility. The valueerror is stating that it can’t convert a string to a float. You may well think that you’re not even using a string. But for better or worse, Python can make it easy to load up a variable whose type is quite different than what you expect.

For example, a string of “5” is a very different thing than an int of 5. But if you print those two variables to screen they’ll look identical. And this is where a lot of the confusion comes from. You’ll most commonly encounter this error when you think you’re working with a numerical value but it’s actually a character-based representation of that number. However, it’s important to take a closer look into how those errors occur before moving on to fix the underlying issue.

A Deeper Look Into Conversions and Valueerrors

We can begin with a Python code example to highlight a common source of the problem. First, try running this script.

x = 12345.6
y = “12345.6”

print(x)
print(y)

print(type(x))
print(type(y))

print(float(x))
print(float(y))

This is a simple example of how Python can have two very different variables that appear and sometimes even function identically. We begin by creating two variables. Line 1 assigns the numerical value of 12345.6 to x. Line 2 assigns a string, “12345.6”, to y.

We proceed to print out x and then y. Following that process, we repeat the print procedure with the output of a type run on both of our variables. There are a few points to note about the script’s output. Most importantly, notice that we were able to use the exact same procedure with both x and y without any error messages. It’s also significant that the print function outputs x and y in exactly the same way. This is because print tries to convert any passed data into a string value. The variable x is passed as a float to print. But the process essentially turns x into the same value as y since x will need to become a string value before it’s output to the terminal.

The two print statements that follow highlight the true disparity between the variables. Type shows the object type of a passed variable. So instead of printing the value of x and y, we’re looking at the output of running type on them. And here we can see that x is a float object but y is a string object.

Finally, we send x and y to the float function and output the result to the screen. Float doesn’t need to do anything with x since the variable is already a float. The y variable is converted to a float by the float function. And both are, in turn, converted to a string through the print function. But now try changing the y declaration to the following.

y = “12,345.6”

When you run the code again you should see the valueerror error message spring up as line 11 tries to convert y to a float. The problem with y is that we’ve declared it with a comma. And float can only work with numerical data or characters which directly correspond to numerical data. Float can convert “1” to 1. But the function doesn’t know what to do with a “,” since there’s no matching numeric value.

In this example code we’d simply need to declare y correctly to fix the problem. But it’s more complex in real-world situations. This particular error is most commonly encountered when reading numbers from a text file or similar source. In those cases, it’d defeat the purpose of using an automated system if we’re manually changing a source document’s values to match our coding style. And even more so if we’re working in data science and reading in hundreds of thousands of data points. But this is fairly easy to fix even in the context of an automated process.

How To Fix the Valueerror

There are a number of different ways to fix this particular error message. But the easiest comes from Python’s regular expressions library. Regular expressions are a way of specifying search patterns in a variable. And we can also use this system to replace characters by using a similar principle. A regular expression can even strip non-numeric characters like a comma from strings through substitution with an empty value. Consider the following example.

import re

y = “12,345.6”
y = re.sub(“[^\d\.]”, “”, y)
print(float(y))

We begin by importing re, Python’s regular expression library. Then we can proceed to declare y in the same way as before. The y value looks like a floating point number at first glance. But y is actually a string with a comma that will prevent it from being successfully converted into a float value. However, on the next line, we perform a regular expression substitution on y and pass the result back to the variable. This removes the comma but not the decimal. Note that this process doesn’t change y into a true numeric value. The y variable hasn’t become a floating point value or an integer value. That’s where the next line comes in.

We finally pass y to float. The function converts y into a float value and passes it to the print function. This process should all run smoothly without any error messages. The reason comes down to the fact that the comma has been removed before sending it to float. You could also move the regular expression into a new function if you were going to deal with this issue on a recurring basis. Likewise, those instances would also benefit from error handling or even notifications that problems are being detected and compensated for.

How to Fix the Python Error: valueerror could not convert string to float
Scroll to top