In this tutorial, we will look at how to check if the user input in Python is empty or not with the help of some examples.
How to check if user input is empty?
Values from the standard input are read as strings in Python. So, to check if the input is empty use the equality operator ==
in Python to check if the input string is equal to an empty string or not.
The following is the syntax –
# check if input is empty s = input() print(s == '')
It returns True
if the input string is empty and False
otherwise
Let’s now look at some examples of using the above syntax –
First, let’s take an empty input using the input()
function and then check if it’s empty or not.
# take input from user s = input() # check if input is emtpy print(s == '')
Output:
True
Here, we gave an empty input. Since the input string is an empty string, we get True
as the output.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Let’s try another example. This time we’ll pass a value to the input.
# take input from user s = input() # check if input is emtpy print(s == '')
Output:
cat False
We get False
as the output since the input string is non-empty.
Note that, if you pass one or more spaces as input, it will be considered as a non-empty string.
# take input from user s = input() # check if input is emtpy print(s == '')
Output:
False
We get False
as the output.
In case, you want to omit the starting and trailing spaces from the entered string, you can the string strip()
function and then check for the empty input.
# take input from user s = input() # check if input is emtpy print(s.strip() == '')
Output:
True
Now, we get True
as the output.
You might also be interested in –
- Python – Check If Input Is a Number
- Python – Check If String Ends with a Newline Character
- Python – Check If String Contains Vowels
- Python – Check If All Characters in String are Same
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.