How to test if object is an integer?

Yingjie Lan lanyjie at yahoo.com
Mon Oct 17 22:50:27 EDT 2011





----- Original Message -----
> From: Noah Hall <enalicho at gmail.com>
> To: MrPink <tdsimpson at gmail.com>
> Cc: python-list at python.org
> 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'



More information about the Python-list mailing list