Check if a String is a Float Number in Python

(Last Updated On: 09/03/2023)

Python Program to Check if a String is a Float Number

# Check if a String is a Float Number in Python

def isfloat(num):
    try:
        float(num)
        return True
    except ValueError:
        return False

# isfloat(), float() tries to convert num to float. If it is successful, then the function returns True.

print(isfloat('f5'))
print(isfloat('12.345'))

Output:

False
True

Leave a Reply

Your email address will not be published. Required fields are marked *