How to check if User Input is Empty in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Check if user input is Empty in Python
  2. Prevent the user from entering only spaces
  3. Prevent empty user input
  4. Prevent empty user input using a while loop
  5. Setting default values on empty user Input in Python
  6. Setting default values on empty user Input using if statement

# 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') else: print('User input is NOT empty')

check if input is empty

The code for this article is available on GitHub

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.

# Prevent the user from entering only spaces

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

main.py
country = input('Where are you from: ') if country.strip() == '': print('User input is empty') else: print('User input is NOT empty')
The code for this article is available on GitHub

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

main.py
print(repr(' '.strip())) # ๐Ÿ‘‰๏ธ '' print(repr(' bobbyhadz.com '.strip())) # ๐Ÿ‘‰๏ธ 'bobbyhadz.com'

# Prevent empty user input

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

prevent empty user input

The code for this article is available on GitHub
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.

# Prevent empty user input using a 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: ')
The code for this article is available on GitHub

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 the input, converts it to a string and returns the result.

# Setting default values on empty user Input in Python

Use the or boolean operator to set a default value on empty user input. The boolean OR operator will return the default value if the input is empty.

main.py
default = 'English' user_input = input('Enter your preferred language: ') or default print(user_input) # ------------------------------------------- default = 100 user_input = int(input('Enter an integer: ') or default) print(user_input)

set default value on empty user input

The code for this article is available on GitHub

We used the boolean or operator to set a default value on empty user input.

The expression x or y returns the value to the left if it's truthy, otherwise the value to the right is returned.

main.py
print('' or 'default value') # ๐Ÿ‘‰๏ธ default value print('hello' or 'default value') # ๐Ÿ‘‰๏ธ hello
The boolean or operator will return the value to the right if the value to the left is falsy.

The input() function returns an empty string if the user didn't enter a value.

Empty strings are falsy, so if the user presses Enter without typing in a value, the default value is returned.

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 the input, converts it to a string and returns the result.

The input() function is guaranteed to return a string, even if the user entered a number.

Here is an example that sets a default value on empty user input when a number is expected.

main.py
default = '100' user_input = int(input('Enter an integer: ') or default) print(user_input)

input with default numeric

We used the int() class to convert the input string to a number.

The or operator returns the default value if the user didn't type in anything.

Alternatively, you can use an if statement.

# Setting default values on empty user Input using if statement

To set a default value on empty user input:

  1. Use the input() function to take input from the user.
  2. Use an if statement to check if the input value is an empty string.
  3. If the input value is an empty string, set it to the default value.
main.py
default = 'English' user_input = input('Enter your preferred language: ') if user_input == '': user_input = default print(user_input)
The code for this article is available on GitHub

We used an if statement to check if the input value is an empty string.

If it is, we reassign the user_input variable to a default value.

This approach is a bit more verbose, however, it might be easier to read if you're not familiar with the boolean or operator.

I've also written an article on how to check if a line is empty.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev