Python: Check if string is empty or blank or contain spaces only

In this article we will discuss different ways to check if a given string is either empty or contains only spaces.

Check if a string is empty using len()

len() function in python accepts a sequence as an argument and returns the number of elements in that sequence. So, if we pass a string as an argument to the len() function, then it returns the total number of characters in that string.

So, we can use this len() function to check if a string is empty or not, by checking if number of characters in the string are zero or not i.e.

msg = ""

# check if string is empty
if len(msg) == 0:
    print('String is empty')
else:
    print('String is not empty')

Output:

String is empty

But if our variable contains None or blank spaces then this solution will not work.

Check if a string is empty using not

An empty string in python is equivalent to False in python. So, to check if a string is empty or not we can just apply “not” operator with it i.e.

msg = ""

# check if string is empty or not
if not msg:
    print('String is empty')
else:
    print('String is not empty')

Output:

String is empty

Unlike previous solution, this solution will work even if variable contains None i.e.

msg = None

# check if string is empty or not
if not msg:
    print('String is empty or None')
else:
    print('String is not empty')

Output

 

String is empty or None

Using this technique we can check if a given string is empty or None. But if string is blank i.e. contains only white spaces then both these solutions will also not work. Let’s discuss different techniques to check if string is empty or contain spaces only,

Check if a string is empty or contain blank spaces only

Using strip():

We can use the strip() function of string to get a copy of string without leading and trailing whites paces. So, let’s use this to check if string is empty or contains only white spaces i.e.

msg = "     "

# Check if string is empty or contain spaces only
if msg and msg.strip():
    print('String is neither empty nor blank')
else:
    print('String is either None or Empty or contain spaces only')

Output:

String is either None or Empty or contain spaces only

It stripped all the white spaces from front and end of the string and converted the blank string to an empty string. Then it check it checked if string is empty or not.

Using isspace()

isspace() function of string class returns True if string contains only white spaces. So we can use this to check if string is empty or contain white spaces only i.e.

msg = "     "

# Check if string is empty or contain spaces only
if msg and not msg.isspace():
    print('String is neither empty nor blank')
else:
    print('String is either None or Empty or Blank')

Output:

String is either None or Empty or contain spaces only

Using Regex to check if a string is empty or contain blank spaces only in python

We can create a regex pattern that checks if the given string is either empty or contains only white spaces i.e.

import re

msg = "     "

# Check if string is empty or contain spaces only
if not msg or re.search("^\s*$", msg):
    print('String is either empty or Blank or contain only spaces')

else:
    print('String is neither empty nor blank')

Output:

String is either None or Empty or contain spaces only

Here we checked if the given string started with zero or more white spaces and contains only whitespaces after that, till the end.

Another example to check if string is empty or contain only spaces, using regex,

import re

msg = ""

# Check if string is empty or contain spaces only
if not msg or re.search("^\s*$", msg):
    print('String is either empty or Blank or contain only spaces')

else:
    print('String is neither empty nor blank')

Output:

String is either None or Empty or contain spaces only

So, here we discussed four different techniques to check if a given string is empty or blank in python.

The complete example is as follows,

import re


def main():
    print('*** Check if a string is empty using len() in Python *** ')

    msg = ""

    # check if string is empty
    if len(msg) == 0:
        print('String is empty')
    else:
        print('String is not empty')

    print('*** Check if a string is empty using "not" operator in python *** ')

    msg = ""

    # check if string is empty or not
    if not msg:
        print('String is empty')
    else:
        print('String is not empty')

    msg = None

    # check if string is empty or not
    if not msg:
        print('String is empty or None')
    else:
        print('String is not empty')

    print('Check if a string is empty by comparing with "" ')

    msg = ""
    if msg == "":
        print('String is empty')
    else:
        print('String is not empty')

    print('*** Check if a string is empty or contain blank spaces only ***')

    print('***** Check if a string is empty or contain blank spaces only using strip() ****')

    msg = "     "

    # Check if string is empty or contain spaces only
    if msg and msg.strip():
        print('String is neither empty nor blank')
    else:
        print('String is either None or Empty or contain spaces only')

    print('***** Check if a string is empty or contain blank spaces only using isspace() ****')

    msg = "     "

    # Check if string is empty or contain spaces only
    if msg and not msg.isspace():
        print('String is neither empty nor blank')
    else:
        print('String is either None or Empty or Blank')

    print('***** Using Regex to check if a string is empty or contain blank spaces only in python ****')

    print('Example 2:')

    msg = "     "

    # Check if string is empty or contain spaces only
    if not msg or re.search("^\s*$", msg):
        print('String is either empty or Blank or contain only spaces')

    else:
        print('String is neither empty nor blank')

    print('Example 2:')

    msg = ""

    # Check if string is empty or contain spaces only
    if not msg or re.search("^\s*$", msg):
        print('String is either empty or Blank or contain only spaces')

    else:
        print('String is neither empty nor blank')


if __name__ == '__main__':
    main()

Output:

*** Check if a string is empty using len() in Python *** 
String is empty
*** Check if a string is empty using "not" operator in python *** 
String is empty
String is empty or None
Check if a string is empty by comparing with "" 
String is empty
*** Check if a string is empty or contain blank spaces only ***
***** Check if a string is empty or contain blank spaces only using strip() ****
String is either None or Empty or contain spaces only
***** Check if a string is empty or contain blank spaces only using isspace() ****
String is either None or Empty or Blank
***** Using Regex to check if a string is empty or contain blank spaces only in python ****
Example 2:
String is either empty or Blank or contain only spaces
Example 2:
String is either empty or Blank or contain only spaces

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