Python Forum
how i can check the input type?
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how i can check the input type?
#1
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
Reply
#2
Try this
answer = input("Enter something: ")
try:
    value = int(answer)
    print("it is an integer")
except ValueError:
    print("it is not an integer")
Reply
#3
(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
Reply
#4
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 403 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  how to make sure an input is from the wanted type astral_travel 16 2,464 Oct-31-2022, 04:11 PM
Last Post: astral_travel
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,784 Nov-23-2020, 11:01 PM
Last Post: perfringo
  isinstance() always return true for object type check Yoki91 2 2,553 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  Check all input, easy way! Help pls! bntayfur 2 1,789 Jul-05-2020, 10:58 PM
Last Post: bntayfur
  check pandas variable type cools0607 3 6,649 Jun-12-2020, 09:28 AM
Last Post: buran
  input check help! bntayfur 3 2,355 Jun-02-2020, 11:38 PM
Last Post: bowlofred
  Type hinting - return type based on parameter micseydel 2 2,473 Jan-14-2020, 01:20 AM
Last Post: micseydel
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,735 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  catch input type error mcmxl22 5 3,033 Aug-11-2019, 07:33 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020