Check if a string contains only whitespace in Python

avatar

Borislav Hadzhiev

Last updated: Aug 21, 2022

banner

Photo from Unsplash

Check if a string contains only whitespace in Python #

Use the str.isspace() method to check if a string contains only whitespace, e.g. if my_str.isspace():. The str.isspace method returns True if there are only whitespace characters in the string and there is at least one character, otherwise False is returned.

main.py
my_str = ' ' # ✅ Check if a string contains only whitespace (str.isspace()) if my_str.isspace(): # 👇️ this runs print('The string contains only whitespace') else: print('The string does NOT only contain whitespace') # ---------------------------------------------------- # ✅ Check if a string does not only contain whitespace if not my_str.isspace(): print('The string does NOT only contain whitespace') else: # 👇️ this runs print('The string contains only whitespace') # ---------------------------------------------------- # ✅ Check if a string contains only whitespace (str.strip()) if my_str.strip() == '': print('The string contains only whitespace')

The first example uses the str.isspace method to check if a string contains only whitespace.

The str.isspace method returns True if the string contains only whitespace characters and there is at least one character in the string, otherwise False is returned.

main.py
print(' '.isspace()) # 👉️ True print(''.isspace()) # 👉️ False print(' a '.isspace()) # 👉️ False

Note that the method returns False if the string is empty.

If you consider an empty string one that only contains whitespace, check for the string's length.

main.py
my_str = ' ' if len(my_str) == 0 or my_str.isspace(): print('The string contains only whitespace')

The example checks if the string is empty or contains only whitespace characters.

We used the boolean or operator, so for the if block to run, either condition has to be met.

Alternatively, you can use the str.strip() method.

Check if a string contains only whitespace using str.strip() #

To check if a string contains only whitespace:

  1. Use the str.strip() method to remove the leading and trailing whitespace from the string.
  2. Check if the string is empty.
  3. If the string is empty with all its whitespace removed, it contains only whitespace.
main.py
my_str = ' ' if my_str.strip() == '': print('The string contains only whitespace')

The str.strip method returns a copy of the string with the leading and trailing whitespace removed.

The method doesn't change the original string, it returns a new string. Strings are immutable in Python.

If the result of calling the str.strip() method on the string returns an empty string, then the string contains only whitespace or is an empty string.

If you want to check if the string contains only whitespace characters and contains at least one character, check if the string is truthy.

main.py
my_str = ' ' if my_str and my_str.strip() == '': print('The string contains only whitespace')

We used the boolean and operator, so for the if block to run, both conditions have to be met.

The first condition checks if the string is truthy.

Empty strings are falsy, so the condition isn't met for an empty string.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.