Loading...

Fix ValueError: could not convert string to float

Fix ValueError: could not convert string to float

According to the rules defined by the Python programming language, a string can be converted into a floating point datatype if it contains only numerical. If it contains anything other characters like commas, spaces, or certain other characters then we face valueerror i.e. “could not convert string to float”.

Introduction?

There are different types of errors in Python like FileNotFoundError, IndexError, NameError, etc. One such type of error is the ValueError. But there are various reasons for ValueError getting raised. One such reason that needs to be addressed is could not convert string to float.


Different reasons to encounter this issue⁉️

This error is encountered if we intend to convert a string that cannot be converted to the float() data type. This kind of conversion is not supported till the latest version of Python i.e. 3.10.7. . We face this issue in various places:

  • Empty string conversion
  • An array of character(s) (strings in python)
# ❌ ValueError: could not convert string to float: '' print(float('')) # ❌ ValueError: could not convert string to float: '3.1417%' print(float('3.1417%')) # ❌ ValueError: could not convert string to float: 'xyz123' print(float('xyz123'))
Code language: Python (python)

Empty String Conversion

Error due to reason – 1

It’s visible from the input that an empty string cannot be converted into a float. This is against the rules of the python language. Therefore it doesn’t work as intended.

Converting a numerical string

Error due to one type of reason – 2

Similarly, if we try to convert a numerical string (in python numbers enclosed in single quotes and terminated with a % is treated as a string) then we experience this issue.

General string Conversion

Error due to one type of reason – 2

Last but not least, a general string cannot be directly converted into a floating data type. That’s the issue the code run into.

P.S.: All the code displayed here is run on the command line hence the file is mentioned as <stdin> (refers to standard input i.e. command line or user-provided input)

How to fix these errors❓

There are 4 methods to fix this issue. Those are:

  • Remove all characters, and symbols, and extract only the number (using re)
  • Remove leading and trailing unwanted characters (using `str.strip`)
  • Replace the unnecessary character(s) with an empty string (using `str.replace`)
  • Using try-catch logic

Remove unwanted characters, symbols

In this approach, we are using regex matching with the help of the `re` module available in python. This is so powerful that it is highly customizable and regex could be fine-tuned as per our needs to extract the numbers alone.

# ? Solution 1 import re input_string = 'x3.1417z' to_convert = re.findall(r'\d+\.\d+', input_string) # Type => ❌ List print(to_convert) # ['3.1417'] # Type => ? Float converted = float(to_convert[0]) print(converted) # 3.1417
Code language: Python (python)

The above script shows the usage of the `re` module to extract only the numbers including the decimal point and convert them to a floating point value. Be cautious while using the findall() function as it generates a list to capture all the matched instances. If there is more than one matched instance then we need to run a loop over to convert all of them to floating point data type.

Removing leading and trailing characters

In this procedure, we use the strip() function which strips off(clears the matched regex). It looks at the entirety of the string and removes it on either side unlike the lstrip() and rstrip() which focus either on the left or right ends respectively.

# ? Solution 2 input_string = '3.1417%' converted = float(input_string.strip('.%')) # Type => ? Float print(converted) # 3.1417
Code language: Python (python)

In this case, the regex looks for both dot and percentage and strips them off after scanning the full length of the string. The same task is being performed above and we get the number alone at the end.

Remove unnecessary characters and replace them with ”

In this technique, we scan through the entire string and replace the non-numerical character(s) with i.e. empty string. It has a length equal to zero and is equivalent to removing the character.

# ? Solution 3 input_string = '3.14%' to_convert = input_string.replace('%', '') converted = float(to_convert) # Type => ? Float print(converted) # 3.14
Code language: Python (python)

Here the percentage(%) symbol is removed and we get a string that contains only numbers, which can easily be converted to float. The same approach is illustrated using the above script.

Using try-catch statements

Python has a try-except logic to deal with errors and ensure the smooth running of the program without termination. This is termed as exceptional handling and is of great use especially in production-ready code so that everything runs without any hiccups.

The same methodology can be utilized to deal with our case as well. The below script gives instinct of the logic:

input_string = '' try: converted = float(input_string) except ValueError: converted = 0 print(converted) # 0 (as except block is executed)
Code language: Python (python)

This method has a huge advantage in that all three previous approaches could be used in the except block and can handle any case with ease. This is more flexible and customizable as per the end-user requirements.


Conclusion?

Dealing with errors is an integral part of a developer’s life and the more the errors are dealt with, the better as a developer you become. The data type-based error (termed) as ValueError is commonly encountered in Python and can have different reasons to arise. So there are different methods to overcome them as well, as discussed above. It’s preferred to choose the try-except logic less assured you don’t need heavy checking.

There is a course offered by Codedamn that teaches you the basics of Python and you can get a clear idea of dealing with data types to overcome ValueError.

Sharing is caring

Did you like what Sriniketh J wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far