Python Program - Check If a String Is a Number (Float)

here's a Python program that checks if a string is a float:

# get input from user
s = input("Enter a string: ")

try:
    # try to convert the string to a float
    float(s)
    print("The string is a float")
except ValueError:
    # handle the exception if the string cannot be converted to a float
    print("The string is not a float")
Source:‮ww‬w.theitroad.com

In this program, we first use the input() function to get input from the user, and store it in the variable s.

We then use a try block to attempt to convert the string to a float using the float() function. If the conversion is successful, we print out the message "The string is a float". If the conversion fails (i.e., if the string is not a valid float), a ValueError exception will be raised, and we will handle the exception in the except block by printing out the message "The string is not a float".