Pythonic way to determine if a string is a number
Scott David Daniels
Scott.Daniels at Acm.Org
Sun Feb 15 19:13:41 EST 2009
python at bdurham.com wrote:
> Thanks for everyone's feedback....
> def isnumber( input ):
> try:
> num = float( input )
> return True
>
> except ValueError:
> return False
Pretty good, but what about 0x23?
def isnumber( input ):
try:
num = float(input)
return True
except ValueError:
try:
num = int(input, 0) # Convert to int, base spec in arg
return True
except ValueError:
return False
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list