ValueError: could not convert string to float in Python


Borislav Hadzhiev

Last updated: Apr 24, 2022


Photo from Unsplash

ValueError: could not convert string to float in Python #

The Python "ValueError: could not convert string to float" occurs when we pass a string that cannot be converted to a float (e.g. an empty string or one containing characters) to the float() class. To solve the error, remove all unnecessary characters from the string.

valueerror could not convert string to float

Here are 3 examples of how the error occurs.
# ⛔️ ValueError: could not convert string to float: '3.14%' print(float('3.14%')) # ⛔️ ValueError: could not convert string to float: '' print(float('')) # ⛔️ ValueError: could not convert string to float: 'abc3.14' print(float('abc3.14'))

The string we are trying to convert to a floating-point number should not contain any characters or symbols.

One way to solve the error is to extract only the float value from the string using the re.findall() method.
import re my_str_1 = 'a3.14b' m = re.findall(r'\d+\.\d+', my_str_1) print(m) # 👉️ ['3.14'] # ✅ convert string to float my_float = float(m[0]) print(my_float) # 👉️ 3.14

The re.findall method takes a pattern and a string as arguments and returns a list of strings containing all non-overlapping matches of the pattern in the string.

Make sure to print() the value you are passing to the float class as it may not contain what you expect.

An alternative approach is to use the str.replace() method to remove all unnecessary characters by replacing them with an empty string.
my_str = '1,2.345%' my_float = float(my_str.replace(',', '').replace('%', '')) print(my_float) # 👉️ 12.345

We replaced the comma and the percent sign with an empty string and successfully converted the string to a floating-point number.

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.
my_str = '3.14%' result = my_str.replace('%', '') print(result) # '3.14'

You can also use the str.strip() method to remove specific leading and trailing characters.
my_str = '.%3.14%.' print(my_str) my_float = float(my_str.strip('%.')) print(my_float) # 👉️ 3.14

The str.strip() method returns a copy of the string with the leading and trailing characters removed.

Characters are removed from the leading and trailing ends until reaching a character that is not contained in the set of specified characters we passed to the method.

You can also use a try/except block to handle the error.
my_str = ' ' try: result = float(my_str) except ValueError: result = 0 print(result) # 👉️ 0

If calling the float() class with the value raises a ValueError, the except block runs where we set the result variable to 0.

Make sure you aren't passing an empty string or a string that contains a space to the float() class.
my_str = ' ' result = float(my_str.strip() or 0) print(result) # 👉️ 0.0

We used the strip() method without passing it any arguments to remove any trailing and leading whitespace.

If removing all leading and trailing whitespace from the string returns an empty string, we pass 0 to the float() class.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.