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
2
  • Checking if a string can be converted to float in Python Commented Aug 6, 2021 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) Commented Aug 7, 2021 at 0:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.