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

Gaute B Strokkenes gs234 at cam.ac.uk
Sun Nov 5 09:50:16 EST 2000


I've been using Python for something slightly more complicated than
simple 'Hello World' programs for the first time in my life.  I find
it very comfortable, but on the other hand I've also got this snippet
in my program:

def isanum(str):
    # FIXME: Surely there must be a sane way of doing this.
    from string import find, digits
    for i in range(len(str)):
        if find(digits, str[i]) == -1:
            return 0
    return 1

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.

-- 
Gaute Strokkenes                        http://www.srcf.ucam.org/~gs234/
I have nostalgia for the late Sixties!  In 1969 I left my laundry with
 a hippie!!  During an unauthorized Tupperware party it was chopped &
 diced!



More information about the Python-list mailing list