How to check if a string "is" an int?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Dec 21 10:09:02 EST 2005


On Wed, 21 Dec 2005 16:39:19 +0100, Daniel Schüle wrote:

> pinkfloydhomer at gmail.com wrote:
>> How do I check if a string contains (can be converted to) an int? I
>> want to do one thing if I am parsing and integer, and another if not.
>> 
>> /David
>> 
> 
> others already answered, this is just an idea
> 
>  >>> def isNumber(n):
> ...     import re
> ...     if re.match("^[-+]?[0-9]+$", n):
> ...             return True
> ...     return False

This is just a thought experiment, right, to see how slow you can make
your Python program run?

*smiles*

Jamie Zawinski: "Some people, when confronted with a problem, think 'I
know, I'll use regular expressions.' Now they have two problems."



-- 
Steven.




More information about the Python-list mailing list