AppDividend
Latest Code Tutorials

How to Check If String is Empty or Not in Python

0

String data type in Python is immutable; that means you can not change a string once defined.  String with spaces is actually an empty string but has non-zero size. So, when you check the empty string with len() or not operator, it counts space as a character in the string, and hence it won’t count a string with space as an empty string.

How to check an empty string in Python

To check an empty string in Python, you can use one of the following methods.

  1. Using not operator
  2. Using len() function
  3. Using not + string.strip()
  4. Using not + string.isspace()

Not operator to check empty string

The not operator in Python checks the string with just spaces to be non-empty, which should not practically be True.

# app.py

strA = ""

# checking if string is empty
print("Check if the strA is empty : ", end="")
if(not strA):
    print("The string is empty")
else:
    print("No it is not empty")

Output

Check if the strA is empty : The string is empty

The string is empty, and one thing to note that we did not even put the space in the string. That is why it returns as empty. If we put a space in the string, it will not count as empty, and the not operator returns False.

# app.py

strA = " "

# checking if string with space is empty
print("Check if the strA with space is empty : ", end="")
if(not strA):
    print("The string is empty")
else:
    print("No it is not empty")

Output

Check if the strA with space is empty : No it is not empty

Using len() function

To check an empty string in Python, use the len() function, and if it returns 0, that means the string is empty; otherwise, it is not.

So, if the string has something, it will count as a non-empty string; otherwise, it is an empty string.

strA = ""

# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(strA)):
    print("The string is not empty")
else:
    print("The string is empty")

Output

Check if the string is empty : The string is empty

Here, if condition becomes False because it returns 0, that means else condition becomes True, and the string is empty.

If the string contains whitespace, then it does not count as an empty string.

strA = " "

# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(strA)):
    print("The string is not empty")
else:
    print("The string is empty")

Output

Check if the string is empty : The string is not empty

Space in string counts as a character. That is why when you check for the empty, it returns False.

Using len() + string.strip()

To check a pure empty string in Python, use len() + string.strip() method. The string.strip() method removes the whitespace from the string. So if it contains any space, then the strip() function removes and then check if the string is empty or not using the len() function.

str = "   "

# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(str.strip())):
    print("The string is not empty")
else:
    print("The string is empty")

Output

Check if the string is empty : The string is empty

Here, no matter how many spaces you will add in the string, it strips all the spaces and checks the length of the string, and if it returns 0, that means the string is empty; otherwise, not.

str = " KRUNAL  "

# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(str.strip())):
    print("The string is not empty")
else:
    print("The string is empty")

Output

Check if the string is empty : The string is not empty

In this example, you can clearly see that the string is not empty cause it has certain characters in it. So len() method returns the length of the string and if condition returns True.

Using not + string.isspace()

The string.isspace() function checks if the string contains any space or not. If the string contains any space, then it returns True. Otherwise, it returns False.

We use the combination of string and not string.isspace() method to check if the string is empty or not regardless of the space.

This method is more efficient than the strip() method because it requires executing the strip operation, which takes computation loading time if the string contains lots of spaces.

str = "  "

# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(str and not str.isspace()):
    print("The string is not empty")
else:
    print("The string is empty")

Output

Check if the string is empty : The string is empty

Here, we are checking the negative condition of isspace() function with and operator. 

If one of the conditions becomes False, then due to an “and operator” if condition returns False and hence else condition will be executed.

This is the better way to check a pure empty string in Python.

That is it for this tutorial.

See also

How to trim whitespace from a string

Python String startswith()

Python String endswith()

Leave A Reply

Your email address will not be published.

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