Python Program to Check If String is Empty or Not

Here are different ways to check if a string is empty or not in Python:

  1. Using not operator
  2. Using len() function
  3. Comparison with an Empty String
  4. Using bool()
  5. Using list comprehension
  6. Using strip() method
  7. 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

Using not operator

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 2: Using len() function

You can use the len() function to find the total number of characters in a string.

It returns True if the length is zero, indicating an empty string; otherwise, it returns False.

Visual Representation

Using len() function

Example

#String Without Space
my_str = ""

if len(my_str) == 0:
 print("The string is empty")
else:
 print("The string is not empty")

#String With Space
my_str_2 = " "

if len(my_str_2) == 0:
 print("The string is empty")
else:
 print("The string is not empty")

#Non-Empty String
my_str_3 = "CR7"

if len(my_str_3) == 0:
 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 3: Comparison with an Empty String

This method is straightforward and explicitly checks whether the string is empty.

It returns True if the string contains no characters, including spaces; otherwise, it returns False.

Visual Representation

Comparison with an Empty String

Example

#String Without Space
my_str = ""

if my_str == "":
 print("The string is empty")
else:
 print("The string is not empty")

#String With Space
my_str_2 = " "

if my_str_2 == "":
 print("The string is empty")
else:
 print("The string is not empty")

#Non-Empty String
my_str_3 = "CR7"

if 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

Using bool()

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

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.