Python Forum

Full Version: how i can check the input type?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi recently i started learning python by my self and i faced this problem :
how i can check the input if it int or a sting?
any help Cry
Try this
answer = input("Enter something: ")
try:
    value = int(answer)
    print("it is an integer")
except ValueError:
    print("it is not an integer")
(Dec-13-2018, 06:27 PM)Gribouillis Wrote: [ -> ]Try this
answer = input("Enter something: ")
try:
    value = int(answer)
    print("it is an integer")
except ValueError:
    print("it is not an integer")

thank you a lot dude Blush this was very helpful
I also use mostly try/except way proposed by @Gribouillis.

But look at the string methods isdecimal, isdigit, isnumeric.

answer = input("Enter something: ")
if answer.isnumeric():
    number = int(answer)
else:
    # do something else.