How To Check If Variable Is Empty In Python?
Last Updated :
14 Feb, 2024
Handling empty variables is a common task in programming, and Python provides several approaches to determine if a variable is empty. Whether you are working with strings, lists, or any other data type, understanding these methods can help you write more robust and readable code. In this article, we will see how to check if variable is empty in Python.
Check If Variable Is Empty In Python
Below are some of the ways by which we can check if the variable is empty in Python or not:
- Using len() function
- Using not Keyword
- Using the None Value
- Using Custom Function
Check If Variable Is Empty Using len() function
In this example, the code checks if the length of the list (`my_list`) is zero using `len()`. If the length is zero, it prints "Empty Variable"; otherwise, it prints "Not Empty Variable," providing a simple way to determine whether the list is empty or not.
Python
my_list = []
if len(my_list) == 0:
print("Empty Variable")
else:
print("Not Empty Variable")
Check If Variable Is Empty Using not Keyword
In this example, the code checks if the string variable `my_list` is empty by evaluating its truthiness. If the string is empty, it prints "Empty Variable"; otherwise, it prints "Not Empty Variable," illustrating a concise way to ascertain whether the string is empty or not.
Python3
my_list = ""
if not my_list:
print("Empty Variable")
else:
print("Not Empty Variable")
Check If Variable Is Empty Using the None Value
In this example, the code checks if the dictionary variable `my_dict` is set to `None`. If the variable is `None`, it prints "empty variable"; otherwise, it prints "not empty variable," demonstrating a straightforward way to determine whether the dictionary is empty or not.
Python3
my_dict = None
if my_dict is None:
print("empty variable")
else:
print("not empty variable")
Check If Variable Is Empty Using Custom Function
In this example, the code defines a function `is_string_empty` that checks whether a given string (`my_string`) is empty. The function returns `True` if the string is `None` or if its stripped version has zero length. Two strings, `my_string1` and `my_string2`, are then evaluated using this function.
Python
def is_string_empty(my_string):
return my_string is None or len(my_string.strip()) == 0
my_string1 = "opkr"
my_string2 = ""
if is_string_empty(my_string1):
print("String 1 is empty.")
else:
print("String 1 is not empty.")
if is_string_empty(my_string2):
print("String 2 is empty.")
else:
print("String 2 is not empty.")
OutputString 1 is not empty.
String 2 is empty.
Similar Reads
How to Check if Tuple is empty in Python ? A Tuple is an immutable sequence, often used for grouping data. You need to check if a tuple is empty before performing operations. Checking if a tuple is empty is straightforward and can be done in multiple ways. Using the built-in len() will return the number of elements in a tuple and if the tupl
2 min read
How to Check if a Variable is a String - Python The goal is to check if a variable is a string in Python to ensure it can be handled as text. Since Python variables can store different types of data such as numbers, lists or text, itâs important to confirm the type before performing operations meant only for strings. For example, if a variable co
3 min read
Check if String is Empty or Not - Python We are given a string and our task is to check whether it is empty or not. For example, if the input is "", it should return True (indicating it's empty), and if the input is "hello", it should return False. Let's explore different methods of doing it with example:Using Comparison Operator(==)The si
2 min read
Check If API Response is Empty in Python In Python programming, determining whether an API response is empty holds importance for effective data handling. This article delves into concise techniques for checking whether the API response is empty or not, enabling developers to efficiently get rid of several data problems and enabling proper
2 min read
Python | Check if any String is empty in list Sometimes, while working with Python, we can have a problem in which we need to check for perfection of data in list. One of parameter can be that each element in list is non-empty. Let's discuss if a list is perfect on this factor using certain methods. Method #1 : Using any() + len() The combinati
6 min read
Check If a Python Set is Empty In Python, sets are versatile data structures used to store unique elements. It's common to need to check whether a set is empty in various programming scenariosPython# Initializing an empty set s = set() print(bool(s)) # False since the set is empty print(not bool(s)) # True since the set is empty
2 min read
Check If a Text File Empty in Python Before performing any operations on your required file, you may need to check whether a file is empty or has any data inside it. An empty file is one that contains no data and has a size of zero bytes. In this article, we will look at how to check whether a text file is empty using Python.Check if a
4 min read
How to Check If a String is Empty or Contains Whitespaces in Python Sometimes, we must check if a string is empty or only contains Whitespaces. This check is useful when we are processing user input or handling data. Another simple way to check if a string is empty is by using the len() function. This checks the length of the string.Pythons = "" if len(s) == 0: prin
1 min read
How to Check if an Index Exists in Python Lists When working with lists in Python, sometimes we need to check if a particular index exists. This is important because if we try to access an index that is out of range, we will get an error. Let's look at some simple ways to check if an index exists in a Python list.The easiest methods to check if g
2 min read
Check if dictionary is empty in Python Sometimes, we need to check if a particular dictionary is empty or not. Python# initializing empty dictionary d = {} print(bool(d)) print(not bool(d)) d = {1: 'Geeks', 2: 'For', 3: 'Geeks'} print(bool(d)) print(not bool(d)) OutputFalse True True False Different Methods to Check if a Dictionary is Em
5 min read