Check if string is empty or not in Python

In this article, you will learn how to check if string is empty or not using different approaches with examples.

Python program to check if string is empty or not

In Python, strings are an array of bytes representing the Unicode characters. Strings are immutable in Python, which means an immutable object is one that, once created, will not change in its lifetime. 

There are various methods to check if a string is empty or not in Python. We will discuss the top 4 approaches in this article.

  1. Using len() method
  2. Using not operator
  3. Using not+str.strip() method
  4.  Using not + str.isspace method

Using len() funtion

The len() method is a built-in function in Python that returns the length of a string. We can use this technique to determine if the string is empty or not. 

If the len() method returns 0, it means the string is empty. Otherwise, it’s not empty.

Example – Check if the string is empty or not using the len() method.

The string with empty spaces is actually an empty string, but the len() method treats whitespaces as Unicode characters and returns the length of the string.

In the below example, we determine if the string is empty or not by checking if the length of the string is equal to 0 using the “if” statement.

# Check if the string is empty or not using len() method

text1 = ""
text2 = "     "
text3 = "Hello World"

print("Length of text1 :", len(text1))
print("Length of text2 :", len(text2))
print("Length of text3 :", len(text3))

if(len(text1) == 0):
    print("String is empty")
else:
    print("String is not empty")

if(len(text2) == 0):
    print("String is empty")
else:
    print("String is not empty")

if(len(text3) == 0):
    print("String is empty")
else:
    print("String is not empty")

Output

Length of text1 : 0
Length of text2 : 5
Length of text3 : 11
String is empty
String is not empty
String is not empty

Using not operator

The not operator can also perform similar to len() method and check if the length of the string is 0 or not internally. 

Again, the not operator considers the whitespaces a non-empty string similar to the len() method, which is invalid.

Example – Check if the string is empty or not using not operator

# Check if the string is empty or not using not operator

text1 = ""
text2 = "     "
text3 = "Hello World"


if(not text1):
    print("String is empty")
else:
    print("String is not empty")

if(not text2):
    print("String is empty")
else:
    print("String is not empty")

if(not text3):
    print("String is empty")
else:
    print("String is not empty")

Output

String is empty
String is not empty
String is not empty

Using not+str.strip() method

In the above method, the strings with whitespaces are considered non-empty strings, and we can overcome this issue by using the strip() method, which truncates the whitespaces at both leading and trailing ends.

In the below example, the strip() method returns true if it encounters whitespaces, thus solving the issue. 

# Check if the string is empty or not using not operator and strip() method

text1 = ""
text2 = "     "
text3 = "Hello World"


if(not (text1 and text1.strip())):
    print("String is empty")
else:
    print("String is not empty")

if(not (text2 and text2.strip())):
    print("String is empty")
else:
    print("String is not empty")

if(not (text3 and text3.strip())):
    print("String is empty")
else:
    print("String is not empty")

Output

String is empty
String is empty
String is not empty

 Using not + str.isspace method

The most efficient way is to use a combination of not and str.isspace() method because the strip() method has to remove the whitespaces, and it’s a costly operation when compared to the issapce() method.

# Check if the string is empty or not using not operator and isspace() method

text1 = ""
text2 = "     "
text3 = "Hello World"


if(not (text1 and  not text1.isspace())):
    print("String is empty")
else:
    print("String is not empty")

if(not (text2 and not text2.isspace())):
    print("String is empty")
else:
    print("String is not empty")

if(not (text3 and  not text3.isspace())):
    print("String is empty")
else:
    print("String is not empty")

Output

String is empty
String is empty
String is not empty
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Rstrip()

Python String rstrip()

Table of Contents Hide rstrip() Syntaxrstrip() Parametersrstrip() Return Value Example 1: Working of rstrip() Example 2 – How to use rstrip() method in real world? The Python String rstrip() method…
View Post
Python List Insert()

Python List insert()

Table of Contents Hide Syntax of List insert() insert() ParametersReturn Value from List insert()Example 1: Inserting an Element to the ListExample 2: Inserting an element at the start and end of…
View Post