Check if a String is Empty in Python

In this Python tutorial, we will learn how to check if a string is empty or not.

Agenda

Check if a string is empty or not using len()

In this method, we will use the len() function to check the length of the string. So if the length of the string is equal to 0, we can say that the given string is empty. Otherwise, it is not. So using if-else statement with len(), we will check whether the string is empty or not.

Example:

# Consider the two strings
string1="Welcome to thisPointer"
string2=""

# Check whether string1 is empty or not.
if(len(string1)==0):
  print("string1 is empty") 
else:
  print("string1 is not empty") 

# Check whether string2 is empty or not.
if(len(string2)==0):
  print("string2 is empty") 
else:
  print("string2 is not empty") 

Output:

Advertisements
string1 is not empty
string2 is empty

From the above output, you can see that string1 is not empty, because it holds a string and string2 is empty.

Check if a string is empty or not using not operator

In this method, we will use the not operator to check whether the given string is empty or not. So using if-else statements, we will check whether the string is empty or not. The not operator returns True, if the string is empty and returns false if the string is not empty.

Example:

# Consider the two strings
string1="Welcome to thisPointer"
string2=""

# Check whether string1 is empty or not.
if(not string1):
  print("string1 is empty") 
else:
  print("string1 is not empty") 

# Check whether string2 is empty or not.
if(not string2):
  print("string2 is empty") 
else:
  print("string2 is not empty") 

Output:

string1 is not empty
string2 is empty

From the above output, you can see that string1 is not empty, because it holds a string. Whereas, string2 is empty.

Check if a string is empty or not using not operator with str.strip()

In this method, we will use the not operator to check whether the given string is empty or not along with the strip() function. It removes for the spaces in a string. Finally, we are implementing this inside an if condition.

Example:

# Consider the two strings
string1="Welcome to thisPointer"
string2=""

# Check whether string1 is empty or not.
if(not (string1 and string1.strip())):
  print("string1 is empty") 
else:
  print("string1 is not empty") 

# Check whether string2 is empty or not.
if(not (string2 and string2.strip())):
    print("string2 is empty") 
else:
  print("string2 is not empty") 

Output:

string1 is not empty
string2 is empty

From the above output, you can see that string1 is not empty, because it holds a string. Also string2 is empty. This solution is usefull for strings which contains only whitespaces.

Summary

In this article, we learned to check if a string is empty or not using three methods. The first method that we discussed is len(). In this method, we will use the len() function to check the length of the string. So if the length of the string is equal to 0, we can say that the given string is empty. In the second method, we used the not operator to check whether the given string is empty or not. In the last method, we used the not operator to check whether the given string is empty or not along with the strip() function. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top