How to test if object is an integer?

Noah Hall enalicho at gmail.com
Mon Oct 17 16:44:51 EDT 2011


On Sat, Oct 15, 2011 at 12:44 AM, MrPink <tdsimpson at gmail.com> wrote:
>
> Is there a function in Python that can be used to test if the value in
> a string is an integer?  I had to make one up for myself and it looks
> like this:
>
> def isInt(s):
>    try:
>        i = int(s)
>        return True
>    except ValueError:
>        return False


There's the isdigit method, for example -

>>> str = "1324325"
>>> str.isdigit()
True
>>> str = "1232.34"
>>> str.isdigit()
False
>>> str = "I am a string, not an int!"
>>> str.isdigit()
False



More information about the Python-list mailing list