DIGIT-ARC read-only archive · snapshot: July 23, 2026

Python: How to check integer ?

4 posts · originally from https://geek.digit.in/community/threads/python-how-to-check-integer.79122/

nileshgr
nileshgr Original post #1
Jan 27, 2008 at 12:46 PM
Hi I am new to Python.

Suppose an app is accepting input then how do I validate that it is integer or not ?
QwertyManiac
Jan 27, 2008 at 1:16 PM
Use a simple try and catch block?

Code:
a = raw_input('Enter an integer: ')
try:
	a=int(a)
except:
	print 'Please enter a valid integer'
	exit(1)
print 'You entered:', a
nileshgr
Jan 27, 2008 at 1:56 PM
Thanks. Well, my usage isn't command line but using mod_python & httpd. I need this for my new upcoming site which is going to be programmed in Python.
QwertyManiac
Jan 27, 2008 at 4:29 PM
Well, basically all I'd said was to try an int() conversion and except its error messages in case it encounters a number/literal not suitable for conversion to base 10. That is how you check for an input being a valid integer convertible.

This method is not "command-line", only my test input was. Learn some more Python before jumping into the line of fire. :)