In this Python tutorial, we will discuss, how to check if a string can be converted into a float.
Table Of Contents
Using Regex to check if string can be converted into a float
In Python, using the regex module’s match() function, we can check if a string matches the given regex pattern or not. We will use a regex pattern to check if string is a float or not. If the match() returns a Match object then it means string can be converted into a float.
Regex Pattern to match float
'[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$'
Let’s see the complete example,
import re def isFloat(strValue): # Check if a string can be converted into a float if (re.match(r'[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$', strValue) is not None): print('Yes, \"', strValue, '\" can be converted into float') else: print('No, \"', strValue, '\" can not be converted into float') isFloat('10') isFloat('10.67') isFloat('10.67 Dollars') isFloat('-10.67') isFloat('10.6e6') isFloat('Total 10.67')
Output:
Yes, " 10 " can be converted into float Yes, " 10.67 " can be converted into float No, " 10.67 Dollars " can not be converted into float Yes, " -10.67 " can be converted into float Yes, " 10.6e6 " can be converted into float No, " Total 10.67 " can not be converted into float
It covered all the formats of floats. So, this is the best method to check if a string can be converted into a float or not.
Frequently Asked:
Using replace() and isdigit() to check if string can be converted into a float
To check if a string can be converted into a float, remove the first dot (.) from the copy of string and then check if all the remaining characters are digit only. If yes, then it means string can be converted into a float.
To remove the first occurrence of ‘.’ from string, replace that with an empty string using replace() function. Then check if all the remaining characters are digit only using the isdigit() function.
Let’s see an example, where we will check if string ‘100.456’ can be converted into a float or not.
Example 1:
strValue='100.456' # Check if a string can be converted into a float if strValue.replace('.', '', 1).isdigit(): print('Yes, this string can be converted into float') else: print('This string can not be converted into float')
Output:
Yes, this string can be converted into float
It shows that the string ‘100.456’ can be converted into a float. Now let’s see a negative example,
Example:2
Let’s see an example, where we will check if string ‘100 Dollars’ can be converted into a float or not.
strValue='100 Dollars' # Check if a string can be converted into a float if strValue.replace('.', '', 1).isdigit(): print('Yes, this string can be converted into float') else: print('This string can not be converted into float')
Output:
This string can not be converted into float
It shows that the string ‘100 Dollars’ can not be converted into a float.
Using float() to check if string can be converted into a float
Try to convert the string into a float using the float() function. If string can be converted into a float, then it will return a float number, otherwise it will raise an exception. So, we can use the try/except block to check if a string contains a float or not.
Let’s see an example, where we will check if string ‘12.56’ can be converted into a float or not.
strValue='12.56' # Check if a string can be converted into a float try: float(strValue) print('Yes, this string can be converted into float') except: print('This string can not be converted into float')
Output:
Yes, this string can be converted into float
It shows that the string ‘12.56’ can be converted into a float.
Summary
In this article, we learned about three techniques to check if a string can be converted to float or not. Thanks