# How to test if object is an integer?

Discussion in 'Python' started by MrPink, Oct 15, 2011.

1. ### MrPinkGuest

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

MrPink, Oct 15, 2011

2. ### Chris AngelicoGuest

On Sat, Oct 15, 2011 at 10:44 AM, MrPink <> 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 some ambiguity in the definition of "is an integer". For
instance, is "0x100" an integer? Is "0800"? If your definition of "is
an integer" is "can be passed to int() without triggering an
exception" (which is probably the most useful), then your above code
is about perfect. The only change I'd make is to not have an isInt
function at all, but simply to try/except at the point where you need
to make the conversion.

ChrisA

Chris Angelico, Oct 15, 2011

3. ### Mathias LafeldtGuest

On Sat, Oct 15, 2011 at 1:44 AM, MrPink <> 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

According to [1], there're more Exceptions to test for:

try:
int(s)
return True
except (TypeError, ValueError, OverflowError): # int conversion failed
return False

rather than avoiding them to avoid cluttering your code with special
cases"

-Mathias

Mathias Lafeldt, Oct 17, 2011
4. ### Noah HallGuest

On Sat, Oct 15, 2011 at 12:44 AM, MrPink <> 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

Noah Hall, Oct 17, 2011
5. ### Ian KellyGuest

On Mon, Oct 17, 2011 at 2:44 PM, Noah Hall <> wrote:
> 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

That works for non-negative base-10 integers. But:

>>> "-1234".isdigit()

False

Cheers,
Ian

Ian Kelly, Oct 17, 2011
6. ### Roy SmithGuest

In article <>,
Mathias Lafeldt <> wrote:

> According to [1], there're more Exceptions to test for:
>
> try:
> int(s)
> return True
> except (TypeError, ValueError, OverflowError): # int conversion failed
> return False

I don't think I would catch TypeError here. It kind of depends on how
isInt() is defined. Is it:

def isInt(s):
"Return True if s is a string representing an integer"

or is it:

def isInt(s):
"Return True if s (which must be a string) represents an integer"

If the latter, then passing a non-string violates the contract, and the
function should raise TypeError. If the former, then you could make
some argument for catching the TypeError and returning False, but I
think the second version is what most people have in mind for isInt().

Can you even get an OverflowError any more in a modern Python?

>>>

int('99999999999999999999999999999999999999999999999999999999999999999')
99999999999999999999999999999999999999999999999999999999999999999L

Roy Smith, Oct 18, 2011
7. ### Chris KaynorGuest

Python 2.6 running on Windows 7:
>>> 99.0**99**99

OverflowError: (34, 'Result too large')
Traceback (most recent call last):
File "<stdin-inspect>", line 1, in <module>
OverflowError: (34, 'Result too large')

However, from the documentation:
"Because of the lack of standardization of floating point exception
handling in C, most floating point operations also aren’t checked."
(http://docs.python.org/library/exceptions.html#exceptions.OverflowError)

Chris

On Mon, Oct 17, 2011 at 5:33 PM, Roy Smith <> wrote:
>
> In article <>,
>  Mathias Lafeldt <> wrote:
>
> > According to [1], there're more Exceptions to test for:
> >
> > try:
> >     int(s)
> >     return True
> > except (TypeError, ValueError, OverflowError): # int conversion failed
> >     return False

>
>
> I don't think I would catch TypeError here.  It kind of depends on how
> isInt() is defined.  Is it:
>
> def isInt(s):
>  "Return True if s is a string representing an integer"
>
> or is it:
>
> def isInt(s):
>  "Return True if s (which must be a string) represents an integer"
>
> If the latter, then passing a non-string violates the contract, and the
> function should raise TypeError.  If the former, then you could make
> some argument for catching the TypeError and returning False, but I
> think the second version is what most people have in mind for isInt().
>
> Can you even get an OverflowError any more in a modern Python?
>
> >>>

> int('99999999999999999999999999999999999999999999999999999999999999999')
> 99999999999999999999999999999999999999999999999999999999999999999L
> --
> http://mail.python.org/mailman/listinfo/python-list

Chris Kaynor, Oct 18, 2011
8. ### Ian KellyGuest

On Mon, Oct 17, 2011 at 6:40 PM, Chris Kaynor <> wrote:
> Python 2.6 running on Windows 7:
>>>> 99.0**99**99

> OverflowError: (34, 'Result too large')
> Traceback (most recent call last):
>   File "<stdin-inspect>", line 1, in <module>
> OverflowError: (34, 'Result too large')
>
> However, from the documentation:
> "Because of the lack of standardization of floating point exception
> handling in C, most floating point operations also aren’t checked."
> (http://docs.python.org/library/exceptions.html#exceptions.OverflowError)

I think what Roy meant was "can you even get an OverflowError from
calling int() any more", to which I think the answer is no, since in
modern Pythons int() will auto-promote to a long, and in Python 3
they're even the same thing.

Ian Kelly, Oct 18, 2011
9. ### Yingjie LanGuest

----- Original Message -----
> From: Noah Hall <>
> To: MrPink <>
> Cc:
> Sent: Tuesday, October 18, 2011 4:44 AM
> Subject: Re: How to test if object is an integer?

> 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
>

There are some corner cases to be considered with this approach:
1. negative integers: '-3'
2. strings starting with '0': '03'
3. strings starting with one '+': '+3'

Yingjie Lan, Oct 18, 2011
10. ### Steven D'ApranoGuest

On Mon, 17 Oct 2011 18:59:44 -0600, Ian Kelly wrote:

> On Mon, Oct 17, 2011 at 6:40 PM, Chris Kaynor <>
> wrote:
>> Python 2.6 running on Windows 7:
>>>>> 99.0**99**99

>> OverflowError: (34, 'Result too large') Traceback (most recent call
>> last):
>> Â  File "<stdin-inspect>", line 1, in <module>
>> OverflowError: (34, 'Result too large')
>>
>> However, from the documentation:
>> "Because of the lack of standardization of floating point exception
>> handling in C, most floating point operations also arenâ€™t checked."
>> (http://docs.python.org/library/

exceptions.html#exceptions.OverflowError)
>
> I think what Roy meant was "can you even get an OverflowError from
> calling int() any more", to which I think the answer is no, since in
> modern Pythons int() will auto-promote to a long, and in Python 3
> they're even the same thing.

You can still get an OverflowError:

>>> inf = float('inf')
>>> int(inf)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: cannot convert float infinity to integer

and similarly for Decimal('inf') as well.

--
Steven

Steven D'Aprano, Oct 18, 2011