[Tutor] Convert String to Int
wesley chun
wescpy at gmail.com
Mon Jan 19 19:36:07 CET 2009
> if type(x) == int: # check its an integer
i also understand that it's "isinstance(x, int)" should be used
instead of the above or "if type(x) is int" but i'm running a blank as
to why at this exact moment... perhaps it's an all-C function?
however, all of these comparisons are only good if 'x' represents the
object of desire. if it's output from raw_input() [in 2.x] or input()
[in 3.x], it won't do any good because 'x' would still be a str. so
before converting to an int, i would go with the isdigit() method,
perhaps x.strip().isdigit() because you don't want to be messed up by
a user who may have inadvertently typed one or more spaces before or
after the number.
hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com
wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
More information about the Tutor
mailing list