Here are different ways to check if a string is empty or not in Python:
- Using not operator
- Using len() function
- Comparison with an Empty String
- Using bool()
- Using list comprehension
- Using strip() method
- Using __eq__ method
Method 1: Using not operator
The not operator evaluates to True if a string is empty, while a non-empty string evaluates to False.
In a Boolean context, a string with only spaces is still considered non-empty.
Visual Representation
Example
#String Without Space
my_str = ""
if not my_str:
print("The string is empty")
else:
print("The string is not empty")
#String With Space
my_str_2 = " "
if not my_str_2:
print("The string is empty")
else:
print("The string is not empty")
#Non-Empty String
my_str_3 = "CR7"
if not my_str_3:
print("The string is empty")
else:
print("The string is not empty")
Output
The string is empty
The string is not empty
The string is not empty
Method 4: Using bool()
The bool() function returns False (falsy) for empty strings and True (truthy) for non-empty strings.
Visual Representation
Example
#String Without Space
my_str = ""
if(bool(my_str)):
print("The string is not empty")
else:
print("The string is empty")
#String With Space
my_str_2 = " " # This will evaluate to True, as spaces are considered content
if(bool(my_str_2)):
print("The string is not empty")
else:
print("The string is empty")
#Non-Empty String
my_str_3 = "CR7"
if(bool(my_str_3)):
print("The string is not empty")
else:
print("The string is empty")
Output
The string is empty
The string is not empty
The string is not empty
Method 5: Using list comprehension
You can use list comprehension in combination with the len() function to check if the string contains any characters.
This will return a list containing a single string, indicating whether the string is empty or not.
Example
#String Without Space
my_str = ""
result = ["The string is not empty" if len(my_str) > 0 else "The string is empty"]
print(result)
Output
['The string is empty']
Method 6: Using strip() method
The strip() method removes any leading and trailing whitespace from a string.
When you apply strip() to a string and then check it in a Boolean context, you can determine if the string is either completely empty or contains only whitespace.
Example
#String Without Space
my_str = ""
if not my_str.strip():
print("The string is empty or contains only whitespace")
else:
print("The string is not empty")
#String With Space
my_str_2 = " "
if not my_str_2.strip():
print("The string is empty or contains only whitespace")
else:
print("The string is not empty")
#Non-Empty String
my_str_3 = "CR7"
if not my_str_3.strip():
print("The string is empty or contains only whitespace")
else:
print("The string is not empty")
Output
The string is empty or contains only whitespace
The string is empty or contains only whitespace
The string is not empty
Method 7: Using __eq__ method
The __eq__ method checks if the string is equal to an empty string.
This approach is generally considered less Pythonic and more verbose compared to using direct comparison operator(=).
Example
#String Without Space
my_str = ""
if my_str.__eq__(""):
print("The string is empty")
else:
print("The string is not empty")
#String With Space
my_str_2 = " "
if my_str_2.__eq__(""):
print("The string is empty")
else:
print("The string is not empty")
#Non-Empty String
my_str_3 = "CR7"
if my_str_3.__eq__(""):
print("The string is empty")
else:
print("The string is not empty")
Output
The string is empty
The string is not empty
The string is not empty
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.