Is there a way to check from a users input what type it is? and do something like so:
if input == str:
do this
elif input == int:
do this
else:
do this
You can use isinstance
to check the type of something -- But you really probably don't want to do this ...
This also depends a lot on how you're getting the input from the user. If you're using raw_input
, then it is always a string and you'll need to try
to convert it to whatever type you want:
try:
x = int(input):
except ValueError:
try:
x = float(input):
except ValueError:
print "No recognizable type!"
One basic principle of python is duck-typing. If an object looks like a duck, sounds like a duck and smells like a duck, then it's a duck (provided you only care what the object looks, smells and sounds like). Ultimately, in python it's best to try
something to see if it is a "duck". If it isn't a duck, then you catch the Exception and handle it (if you know how). This is the primary reason you don't see people using isinstance
very much in their code and the reason I warn against using it.
you can use isinstance
http://docs.python.org/2/library/functions.html#isinstance
example
isinstance('hello', str)
returns True
If you mean keyboard input, then all input is a string. Even if the user types in a number like "88", the result will be the string "88"
and not the integer 88
. You can, however, use this pattern to tell if the string contains digits and nothing else:
if input.isdigit():
print "input is a number"
else:
print "input is not a number"
Even better is to cast the input to a number, which will handle input like "50.0" or "-10" as mentioned in the comments (if decimals are valid, then use float() or Decimal() instead of int() here):
try:
number = int(input)
except ValueError:
print "input is not a number"
If you aren't referring to keyboard input, then there are various ways to check types, but in general the best thing to do is to simply use the input as if it were valid and catch exceptions that will arise if it is not valid. That way, you define your input requirements in terms of the actual methods and attributes of the object and not simply its nominal class (this is "duck typing").
raw_input
is always a string, while input
(quoting from the docs) is eval(raw_input(prompt))
, so it could be an integer right away. Although I think that the original question is about raw_input :)input()
returns a string and you have to eval() explicitly if that is what you want). The use of input()
in Python 2.x is discouraged.I think you're looking for isinstance
.
You beat me, but mine comes with a link:
http://docs.python.org/2/library/functions.html#isinstance
As an aside, I've been test driving PyCharm IDE, and they'll do type checking for you automagically if you assert isinstance()
in your code, then it does auto-completions. It also allows you to specify type arguments in the docstring. It then uses this type information to help you with auto completes down the road. Pretty handy...
input
(although I suppose in Python 2 overriding a builtin you shouldn't use anyway isn't the worst idea.)