isNumber? check

Gerrit Holl gerrit at
Mon Sep 29 22:41:43 CEST 2003

Rob Hunter wrote:
> On Monday, September 29, 2003, at 10:20 AM, Gerrit Holl wrote:
> >Rob Hunter wrote:
> >>How do I check if a value is a number in Python?
> >>
> >>One way is (x == type(1)) and (x == type(1.2)) and (x ==
> >>type(2387482734274)) and ...
> >>
> >>but this seems kludgy.  Any better way?
> >
> >Why do you want to do so?
> I am writing an interpreter, and my parser has to decide if an 
> expression is a number or not.

So you actually have a string...

Maybe you could do something like this:

if expr.strip("0123456789.e") == "": print "May be a number!"
if expr.strip("0123456789") == "": print "Surely a number!"


249. If any one hire an ox, and God strike it that it die, the man who
hired it shall swear by God and be considered guiltless.
        -- 1780 BC, Hammurabi, Code of Law
Asperger Syndroom - een persoonlijke benadering:
Het zijn tijden om je zelf met politiek te bemoeien:

More information about the Python-list mailing list