What Is The Canonical Way To Check The Type Of Input?
I want to check the type of input on the screen by using python types module I have used the type(2) is int for integer. ---->>>working I have used the type('Vivek') is s
Solution 1:
It up to you to convert your input to whatever you need.
But, you can guess like this:
import sys
p = raw_input("Enter input")
if p.lower() in ("true", "yes", "t", "y"):
p = Trueelif p.lower() in ("false", "no", "f", "n"):
p = Falseelse:
try:
p = int(p)
except ValueError:
try:
p = float(p)
except ValueError:
p = p.decode(sys.getfilesystemencoding()
This support bool
, int
, float
and unicode
.
Notes:
- Python has no
char
type: use a string of length 1, - This
int
function can parseint
values but alsolong
values and even very long values, - The
float
type in Python has a precision of adouble
(which doesn't exist in Python).
See also: Parse String to Float or Int
Post a Comment for "What Is The Canonical Way To Check The Type Of Input?"