Pythonic way to determine if a string is a number
lists at cheimes.de
Sun Feb 15 12:51:57 EST 2009
python at bdurham.com schrieb:
> What's the Pythonic way to determine if a string is a number? By
> number I mean a valid integer or float.
> I searched the string and cMath libraries for a similar function
> without success. I can think of at least 3 or 4 ways to build my
> own function.
> Here's what I came up with as a proof-of-concept. Are there
> 'better' ways to perform this type of test?
> def isnumber( input ):
> if '.' in input:
> num = float( input )
> num = int( input )
> return True
> except ValueError:
> return False
You code doesn't check for several float representations like "1e10",
"1E10", "inf" and "nan".
More information about the Python-list