Last updated: Apr 9, 2024
Reading timeยท4 min
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.
country = input('Where are you from: ') if country == '': print('User input is empty') else: print('User input is NOT empty')
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 a scenario where the
user enters only whitespace characters.
country = input('Where are you from: ') if country.strip() == '': print('User input is empty') else: print('User input is NOT empty')
The str.strip() method returns a copy of the string with the leading and trailing whitespace removed.
print(repr(' '.strip())) # ๐๏ธ '' print(repr(' bobbyhadz.com '.strip())) # ๐๏ธ 'bobbyhadz.com'
The second example uses a while
loop to keep prompting the user until they
enter a non-empty value.
while True: country = input('Where are you from: ') if country.strip() != '': print(country) break
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.
while
loopYou can also use a while loop that iterates until the user enters a value.
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 the input, converts it to a string and returns the result.
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.
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)
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.
print('' or 'default value') # ๐๏ธ default value print('hello' or 'default value') # ๐๏ธ hello
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.
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.
default = '100' user_input = int(input('Enter an integer: ') or default) print(user_input)
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.
if
statementTo set a default value on empty user input:
input()
function to take input from the user.if
statement to check if the input value is an empty string.default = 'English' user_input = input('Enter your preferred language: ') if user_input == '': user_input = default print(user_input)
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.
You can learn more about the related topics by checking out the following tutorials: