0

I can check integer with isdigit() or isnumeric(), but they don't work for float.

    variable = input("Enter anything: \n")
    #entered a float number: 3.1
    print(variable.isdigit()) #returns false
    
    if variable.isnumeric() and variable.find('.')==-1: #isnumeric also returns false
        variable = int(variable)
    elif variable.isnumeric() and variable.find('.')>=0:
        variable = float(variable)
    
    print("is it a string? : ")
    print(isinstance(variable, str)) #TRUE
    print("is it an integer? : ")
    print(isinstance(variable, int)) #false
    print("Is it a float number? : ")
    print(isinstance(variable, float)) #FALSE
SamaSamrin
  • 79
  • 2
  • 11
  • [Checking if a string can be converted to float in Python](https://stackoverflow.com/questions/736043/checking-if-a-string-can-be-converted-to-float-in-python) – Алексей Р Aug 06 '21 at 18:07
  • Figured it out. First used **isalpha()** to see if it is string. If not string, used **isnumeric()** to check if it is an integer. if none of these returns true, then it is a float `a = input("Enter the first number: \n") if a.isalpha(): print("First input is not a number") a = input("Enter a new number: \n") elif a.isnumeric(): a = int(a) else: a = float(a)` – SamaSamrin Aug 07 '21 at 00:33

0 Answers0