One of the most notable breakthrough that Python brought was that the interconversion between the datatypes was done in a very easy manner and hence making it quite powerful. String can be converted to integers easily, but converting a float value is still difficult task. Let’s discuss certain ways in which one can check if string is a float to avoid potential errors.
Method #1 : Using isdigit() + replace()
The combination of above function is used to perform this task and hence. This works in 2 steps, first the point value is erased and the string is joined to form a digit and then is checked. The drawback is that this doesn’t check for potential exponent values that can also form a float number.
# Python3 code to demonstrate # Check for float string # using isdigit() + replace() # initializing string test_string = "45.657" # printing original string print("The original string : " + str(test_string)) # using isdigit() + replace() # Check for float string res = test_string.replace('.', '', 1).isdigit() # print result print("Is string a possible float number ? : " + str(res)) |
The original string : 45.657 Is string a possible float number ? : True
Method #2 : Using float() + Exception handling
This task can also be achieved using the float function which tries to convert the string to floating point value, and it’s failure guarantees that it’s not potential float value.
# Python3 code to demonstrate # Check for float string # using float() # initializing string test_string = "45.657" # printing original string print("The original string : " + str(test_string)) # using float() # Check for float string try : float(test_string) res = Trueexcept : print("Not a float") res = False # print result print("Is string a possible float number ? : " + str(res)) |
The original string : 45.657 Is string a possible float number ? : True
Recommended Posts:
- Convert String float to float list in Python
- Python - Convert Float String List to Float Values
- Python - Summation of float string list
- Python | Min/Max value in float string list
- Python | List of float to string conversion
- Python - Filter float strings from String list
- Python | Convert Joint Float string to Numbers
- Convert String to Float in Python
- Convert Python String to Float datatype
- float() in Python
- Python | Sort a tuple by its float element
- Python program to convert float decimal to Octal number
- Python | Float type and its methods
- Python | Float Layout in Kivy
- Python | Ways to sort list of float values
- Python | Filter out integers from float numpy array
- Python | sympy.Float() method
- Python | Convert tuple to float value
- Python - Convert Float to digit list
- Python | Average of Float Numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

