How to Handle Empty User Input in Python
Validating and handling empty user input is essential for creating robust and user-friendly Python applications.
This guide explores how to check for empty input, prevent users from entering empty values, and set default values when input is empty, ensuring that your programs behave correctly.
Checking for Empty User Input
The input()
function in Python always returns a string. To check if the user provided any input, compare the result to an empty string:
country = input('Where are you from: ')
if country == '':
print('User input is empty')
else:
print('User input is NOT empty')
Preventing Input with Only Whitespace
To prevent input consisting solely of whitespace (spaces, tabs, newlines), use the strip()
method:
country = input('Where are you from: ')
if country.strip() == '':
print('User input is empty')
else:
print('User input is NOT empty')
strip()
removes leading/trailing whitespace. If the result is an empty string the input was either completely empty, or it contained only whitespace characters.
Preventing Empty Input with a while
Loop
To continuously prompt the user until valid (non-empty) input is provided, use a while
loop:
while True:
country = input('Where are you from: ')
if country.strip() != '':
print(country) # Process valid input
break # Exit loop
- The loop continues until the user provides input with at least one non-whitespace character.
- The
break
keyword is used to exit the loop.
Alternatively, a while
loop can continue for as long as a condition is met:
country = ''
while country.strip() == '':
country = input('Where are you from: ')
print(country)
- The loop keeps asking for input until a non-empty string is entered.
Setting Default Values on Empty Input
To assign default values when no input is given you can use the or
operator or the if
statement.
Using the or
Operator
The or
operator provides a concise way to set a default if a variable is an empty string, or any other falsy value.
default = 'English'
user_input = input('Enter your preferred language: ') or default
print(user_input) # Output: (User input or default)
default_num = 100
user_input_num = int(input('Enter an integer: ') or default_num)
print(user_input_num) # Output: (User input or default)
- If the input is empty or contains only whitespace characters the expression
input() or default
will return the default value. - This will work regardless of the type of default variable because if the user input is an empty string (falsy) the value after the
or
operator will be returned. - You must convert the user input to an integer when an integer is expected using
int()
.
Using an if
Statement
If you need a more explicit solution, or the logic becomes more complex, you can use an if statement:
default = 'English'
user_input = input('Enter your preferred language: ')
if user_input == '': # or if not user_input.strip():
user_input = default
print(user_input) # Output: (User input or default)
- This checks explicitly if the user input is an empty string.
- If it is empty a
default
value is assigned.