Best way to check if string is an integer?
andreww at datanet.ab.ca
Sun Apr 6 01:21:01 CEST 2008
skanemupp at yahoo.se wrote:
>which is the best way to check if a string is an number or a char?
>could the 2nd example be very expensive timewise if i have to check a
>lot of strings?
>value = raw_input()
> value = int(value)
> print "value is not an integer"
>if c in '0123456789':
> print "integer"
> print "char"
>or some other way?
I always do it the first way. It is simpler, and should be faster. Also,
the second way will only work on single-digit numbers (you would have to
iterate over the entire string with a for loop to use it on numbers with
more than one digit).
More information about the Python-list