How to check if a string can be converted to float in Python?


In this article, we are going to focus on checking if a string can be converted to a float in Python.

The first approach is by using the float() type cast. We will write a function using exception handling and check if the given string can be converted into string, if it can be converted then True is returned, else False is returned.

A floating−point number is returned by the method float() for a given number or text. It will return 0.0 as the floating−point value if no value or a blank parameter is given.

Example 1

In this example, we are taking a string as input and we are checking if it can be converted into float using the float() type casting 

def isFloat(s):
   try:
      float(s)
      return True
   except:
      return False
      
str1 = "36.9"
print("The given string is")

print(str1)
print("Checking if the given string can be converted into float")

res = isFloat(str1)
print(res)

Output

The output of the above example is as shown below −

The given string is
36.9
Checking if the given string can be converted into float
True

Example 2

In the example given below, we are taking the same program as above and we are taking different input and checking if it can be converted to float 

def isFloat(s):
   try:
      float(s)
      return True
   except:
      return False
      
str1 = "Welcome"
print("The given string is")

print(str1)
print("Checking if the given string can be converted into float")

res = isFloat(str1)
print(res)

Output

The output of the above example is given below −

The given string is
Welcome
Checking if the given string can be converted into float
False

Using isdigit() and replace() method

The second approach is by using isdigit() and replace(). We will replace the ‘.’ present in the float with blank space and checks if the resultant string with isdigit(). If all the characters are digit then True is returned, else False is returned.

Example 1

In this example, we are taking a string as input and checking if it can be converted to float using isdigit() and replace() methods 

str1 = "69.3"
print("The give string is")

print(str1)
print("Checking if the given string can be converted into float")

res = str1.replace('.', '', 1).isdigit()
print(res)

Output

The output of the above example is as shown below −

The give string is
69.3
Checking if the given string can be converted into float
True

Example 2

In the example given below, we are taking the same program as above but we are taking a different input and checking if it can be converted into a float 

str1 = "Welcome"
print("The give string is")

print(str1)
print("Checking if the given string can be converted into float")

res = str1.replace('.', '', 1).isdigit()
print(res)

Output

The output of the above example is as follows −

The give string is
Welcome
Checking if the given string can be converted into float
False

Updated on: 07-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements