Check if user input is an Integer in Python

avatar

Borislav Hadzhiev

Last updated: Aug 22, 2022

banner

Photo from Unsplash

Check if user input is an Integer in Python #

Use a try/except statement to check if a user input is an integer. If the input value is an integer, the try block completes running, otherwise a ValueError is raised which can be handled in the except block.

main.py
# ✅ Check if user input is an integer (try/except) integer = input('Enter an integer: ') try: integer = int(integer) print(integer) except ValueError: print('The provided value is not an integer') # ----------------------------------------- # ✅ Keep prompting the user until they enter an integer (while loop) integer = 0 while True: try: integer = int(input("Enter an integer: ")) except ValueError: print("Please, enter a valid integer") continue else: print(f'You entered: {integer}') break

The first example uses a try/except statement to check if the input value is a valid integer.

If we pass a value that is not a valid integer to the int() class, a ValueError is raised.

main.py
integer = input('Enter an integer: ') try: integer = int(integer) print(integer) except ValueError: print('The provided value is not an integer')

check if input is integer

If the user entered an integer, the try block completes and the except block doesn't run.

If the provided value is not a valid integer, the except block runs.

Alternatively, you can use the str.isdigit() method.

Check if user input is an Integer using str.isdigit() #

Use the str.isdigit() method to check if a user input is an integer. The isdigit() method will return True for all positive integer values and False for all non-numbers, floating-point numbers or negative numbers.

main.py
integer = input('Enter an integer: ') if integer.isdigit(): print('Provided value is an integer') integer = int(integer) print(integer) else: print('Provided value is not an integer')

The str.isdigit method returns True if all characters in the string are digits and there is at least 1 character, otherwise False is returned.

main.py
print('100'.isdigit()) # 👉️ True print('1.5'.isdigit()) # 👉️ False print('-100'.isdigit()) # 👉️ False

Note that the str.isdigit() method returns False for negative integers and floating-point numbers.

This is because negative integers contain a minus sign and floating-point numbers contain a period.

You should only use this approach to check if a user input is a positive integer.

You can use a while loop if you need to keep prompting the user until they enter a valid integer.

Keep asking for user input until an Integer is entered #

The following example uses a while True loop to keep iterating until the user enters a valid integer.

main.py
integer = 0 while True: try: integer = int(input("Enter an integer: ")) except ValueError: print("Please, enter a valid integer") continue else: print(f'You entered: {integer}') break

keep asking for user input until integer given

If the code in the try block raises a ValueError, the except block runs, where we use the continue statement to continue to the next iteration.

If the user enters a valid integer, the try block completes successfully and then the else block runs where we use the break statement to exit out of the while loop.

The continue statement continues with the next iteration of the loop.

The break statement breaks out of the innermost enclosing for or while loop.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.