checking if a string contains a number

Steven D'Aprano steve at REMOVETHIScyber.com.au
Tue Dec 20 16:57:21 EST 2005


On Tue, 20 Dec 2005 15:55:37 +0100, egbert wrote:

> On Tue, Dec 20, 2005 at 07:16:46PM +0530, Suresh Jeevanandam wrote:
>> 	s1 = '12e3'
>> 	s2 = 'junk'
>> 	Is there any other function which would return True for s1 and False 
>> for s2.
>> 
> 
> isinstance(12e3, (int, float))

That won't work, because s1 is a *string*, not a float. The original
poster is asking how to check whether the string can be converted to a
float successfully before actually trying to convert it to a float.


-- 
Steven.




More information about the Python-list mailing list