Check if user input is Empty in Python

avatar

Borislav Hadzhiev

Last updated: Aug 22, 2022

banner

Photo from Unsplash

Check if user input is Empty in Python #

Use an if statement to check if a user input is empty, e.g. if country == '':. The input() function is guaranteed to return a string, so if it returns an empty string, the user didn't enter a value.

main.py
country = input('Where are you from: ') if country == '': print('User input is empty') # ----------------------------------------- # ✅ Prevent user from entering empty input while True: country = input('Where are you from: ') if country != '': print(country) break # ----------------------------------------- # ✅ Using a try/except statement try: num = float(input('Enter your favorite number: ')) print(num) except ValueError: print('Empty input or not a number entered')

The first example uses an if statement to check if a user input is empty.

We directly check if the user didn't enter anything.

You can use the str.strip() method if you need to cover the scenario where the user enters only whitespace characters.

main.py
country = input('Where are you from: ') if country.strip() == '': print('User input is empty')

The str.strip method returns a copy of the string with the leading and trailing whitespace removed.

main.py
print(repr(' '.strip())) # 👉️ '' print(repr(' apple '.strip())) # 👉️ 'apple'

The second example uses a while loop to keep prompting the user until they enter a non-empty value.

main.py
while True: country = input('Where are you from: ') if country.strip() != '': print(country) break
The while loop keeps running until the user enters at least one, non-whitespace character.

On each iteration, we check if the user entered at least one character.

If the condition is met, we use the break statement to exit the loop.

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

You can also use a while loop that iterates until the user enters a value.

main.py
country = '' while country.strip() == '': country = input('Where are you from: ')

We used a while loop to iterate until the country variable doesn't store an empty string.

The input function takes an optional prompt argument and writes it to standard output without a trailing newline.

The function then reads the line from input, converts it to a string and returns the result.

You can also use a try/except statement to handle an empty input exception caused by trying to convert an empty string to an integer or a float.

main.py
try: num = float(input('Enter your favorite number: ')) print(num) except ValueError: print('Empty input or not a number entered')

The float() and int() classes raise a ValueError if passed a value that cannot be converted to the specific type or an empty string.

You can use a while loop if you want to prompt the user until they enter 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

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.

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

If the user enters a valid number, the try block runs successfully and then the else block runs where we use the break statement to exit out of the 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.