Check if user input is a Number in Python

avatar

Borislav Hadzhiev

Last updated: Aug 22, 2022

banner

Photo from Unsplash

Check if user input is a Number in Python #

Use a try/except statement to check if a user input is a number. If the input value is a number, 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 number (try/except statement) num = input('Enter your favorite number: ') try: num = float(num) print(num) except ValueError: print('The provided value is not a number') # ----------------------------------------- # ✅ Keep prompting the user until they enter a number (while loop) num = 0 while True: try: num = int(input("Enter your favorite integer: ")) except ValueError: print("Please enter a valid integer") continue else: print(f'You entered: {num}') break

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

Calling the float() or int() classes with a value that is not a valid number raises a ValueError.

main.py
num = input('Enter your favorite number: ') try: num = float(num) print(num) except ValueError: print('The provided value is not a number')

check if input is number

If the user entered a number, the try block completes and the except block doesn't run.

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

If you only need to check if the user input is a valid integer, use the int() class for the conversion.

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

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

Check if user input is a Number using str.isdigit() #

Use the str.isdigit() method to check if a user input is a number, e.g. if num.isdigit():. 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
num = input('Enter your favorite number: ') if num.isdigit(): print('Provided value is a valid number') num = int(num) print(num) else: print('Provided value is not a number, is a negative number or a float')

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('567'.isdigit()) # 👉️ True print('3.14'.isdigit()) # 👉️ False print('-5'.isdigit()) # 👉️ False

Note that the str.isdigit() method returns False for floating-point numbers (they contain a period) and for negative numbers (they contain a minus sign).

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

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

Keep asking for user input until a Number is entered #

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

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

keep asking for input until valid number 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 number, 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.