How to check if a string "is" an int?
Daniel Schüle
uval at rz.uni-karlsruhe.de
Wed Dec 21 10:39:19 EST 2005
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
does not recognize 0x numbers, but this is easy to fix
if wanted
>>> def isNumber(n):
... import re
... if re.match("^[-+]?[0-9A-Fa-f]+$", n):
... return True
... return False
hth
Daniel
More information about the Python-list
mailing list