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

Paul Prescod paulp at ActiveState.com
Sun Nov 5 11:37:03 EST 2000


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

 Paul Prescod





More information about the Python-list mailing list