checking if a string contains a number

Larry Bates larry.bates at websafe.com
Tue Dec 20 09:08:49 EST 2005


You should probably work through the tutorials.

Use a try block:

try: x=float(s)
except ValueError:
    print 'Non-numeric value %s found' % s

-Larry Bates

Suresh Jeevanandam wrote:
> Hi,
>     I have a string like,
>     s1 = '12e3'
>     s2 = 'junk'
> 
>     Now before converting these values to float, I want to check if they
> are valid numbers.
> 
>     s1.isdigit returns False.
> 
>     Is there any other function which would return True for s1 and False
> for s2.
> 
> Thanks
> 
> -- 
> Suresh



More information about the Python-list mailing list