How can I tell when a string is in fact a number?

Gaute B Strokkenes gs234 at cam.ac.uk
Sun Nov 5 13:04:21 EST 2000


Paul Prescod <paulp at ActiveState.com> writes:

> On 5 Nov 2000, Gaute B Strokkenes wrote:
> >
> > def isanum(str):
> > 
> > As the comment says, I'm sure there must be a more straightforward way
> > to do this.  However, I can't find out how, though I'm sure that it is
> > really my relative unfamiliarity with Python that is to blame.
> 
> There may be a more elegant way to do it, but:
> 
> def isanum(num):
>     try:
>         int(num)
>         return 1
>     except ValueError:
>         return 0

Thanks.  I must admit that expected there to be a simple function to
do this, though...

-- 
Big Gaute                               http://www.srcf.ucam.org/~gs234/
There's a lot of BIG MONEY in MISERY if you have an AGENT!!



More information about the Python-list mailing list