How To Check if a string is Empty in Python?

Rate this post

Summary: Use the len(str) method to check whether the string is empty or not. If the len method returns 0 it implies that the string is empty. Otherwise it is not empty.

Problem Statement: Given a string how to check if it is empty.

Note: An empty string is a string that does not contain any character. This means it does not even contain whitespace characters like space or tab. While a string that contains a single white space character is considered as a blank string.

A Quick look at the solutions:

empty_string = ""
blank_string = " "
# Method 1
if len(empty_string) == 0:
    print("Empty string!")
# Method 2
if not empty_string:
    print(("Empty String"))
# Method 3
if len(blank_string.strip()) == 0:
    print("Blank String converted to Empty String!")
# Method 4
if blank_string and blank_string.isspace():
    print("Blank String converted to Empty String!")

Video Solution:

Let’s dive into the numerous methods of checking if a string is empty or not in Python.

Method 1- Using len()

Python’s built-in function len() returns the length of the given string, array, list, tuple, dictionary, or any other iterable. The type of the return value is an integer that represents the number of elements in this iterable. Hence, we can use this method to check whether the string is empty or not. If the len() method returns 0, it implies that the string is empty.

Python len() - Visual Explanation

Solution:

# Given string
text_1 = ""
l = len(text_1)
if l == 0:
    print("text_1 is an empty string!")
else:
    print("text_1 is not an empty string!")

# Non-empty string
text_2 = "hello"
l = len(text_2)
if l == 0:
    print("text_2 is an empty string!")
else:
    print("text_2 is not an empty string!")

Output:

text_1 is an empty string!
text_2 is not an empty string!

However, this approach only works if the string is empty, i.e. length of the string is 0. If the string contains a single space, it is not treated as an empty string.

# Given string with spaces
s = "    "
l = len(s)
if l == 0:
    print("Given string is empty")
else:
    print(" Given string is not empty")

# Given string is not empty

Related Video [Python’s len() method ]

Method 2- Using not Operator

The not operator works like the len() method in Python. It returns True if the string length is 0 and False otherwise. However, in this method, the string is not considered as an empty string if it contains spaces in it.

Here’s a supporting video to clear your concepts w.r.t not operator in Python.

Solution:

# Given empty string
empty_str = ""
if not empty_str:
    print("Given string is empty")
else:
    print("Given string is not empty")
# Given string with spaces
space_str = "  "
if not space_str:
    print("Given string is empty")
else:
    print("Given string is not empty")
# Given string
word = "test"
if not word:
    print("Given string is empty")
else:
    print("Given string is not empty")

Output:

Given string is empty
Given string is not empty
Given string is not empty

Alternatively, you can use the “if” statement without the not operator to check if the string is empty or not. Look at the following code:

# Given empty string 
s = ""
if s:
    print("Given string is not empty")
else:
    print("Given string is empty")
# Given string with spaces 
st = "  "
if st:
    print("Given string is not empty")
else:
    print("Given string is empty")
# Given string
sr="hello"
if sr:
    print("Given string is not empty")
else:
    print("Given string is empty")

Output:

Given string is empty
Given string is not empty
Given string is not empty

In the next approach(s), we will deal with blank strings and find out how we can convert them to empty strings if needed.

📚Recommended Read: A Simple Guide To Remove Multiple Spaces In A String

Method 3- Using not And strip

As observed in the previous approaches, the string with a single space was not considered as an empty string. Logically speaking, this is correct as space is counted as a character. However, you might want to consider it as an empty string in some cases. There’s a workaround for this.

We can use the strip() method to eliminate the whitespaces and then check if the string is empty or not with the help of the not operator.

Example:

# Given empty string 
s = ""
if not s.strip():
    print("Given string is empty")
else:
    print("Given string is not empty")
# Given string with spaces 
st = "  "
if not st.strip():
    print("Given string is empty")
else:
    print("Given string is not empty")

# Given string contains text
sr = "hello"
if not sr.strip():
    print("Given string is empty")
else:
    print("Given string is not empty")

Output:

Given string is empty
Given string is empty
Given string is not empty

Method 4- Using isspace() with not

The isspace() method in Python gets used to check if the string has any spaces in it. The isspace() method returns True if the string contains only whitespaces, whereas it will return False if the string contains even a single non-space character.

The isspace() method is more efficient than the strip() method as the strip operation usually takes computation loads if the number of spaces is more. 

Example:

# Given empty string 
s = ""
if not (s and not (s.isspace())):
    print("Given string is empty")
else:
    print("Given string is not empty")
# Given string with spaces 
st = "  "
if not (st and not (st.isspace())):
    print("Given string is empty")
else:
    print("Given string is not empty")
    
# Given string
sr = "hello"
if not (sr and not (sr.isspace())):
    print("Given string is empty")
else:
    print("Given string is not empty")

Output:

Given string is empty
Given string is empty
Given string is not empty

Explanation: In the above example we are simply checking if the given string has any character in it or not. When the string stores a space, it does consist of a single character. In this case we also check another condition wherein we check if this character is a space or not. If this is not a space then we can conclude that it is not an empty string else we can conclude it is an empty string. We are simply using negation to our benifit in this situation.

Note:

if (st and not (st.isspace())) == False 
is the same as: if not (st and not (st.isspace()))

Related Video:

Conclusion

In this quick tutorial, we looked at the different methods to check if the string is empty in Python. I hope this article has helped you. Please stay tuned and subscribe for more interesting discussions in the future.

Post Credits: Shubham Sayon and Rashi Agarwal


Finxter Computer Science Academy

  • One of the most sought-after skills on Fiverr and Upwork is web scraping. Make no mistake: extracting data programmatically from websites is a critical life skill in today’s world that’s shaped by the web and remote work.
  • So, do you want to master the art of web scraping using Python’s BeautifulSoup?
  • If the answer is yes – this course will take you from beginner to expert in Web Scraping.