
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if a string contains only whitespace letters in Python?
A string is considered to contain only whitespace if it consists entirely of characters like space (' '), tab ('\t'), newline ('\n'), carriage return ('\r'), etc. Python provides ways to check this directly using built-in functions or regular expressions.
Using isspace() Method
The isspace() method returns True if all characters in the string are whitespace characters and the string is not empty. If there is any non-whitespace character, it returns False.
Example: Only Whitespace
In the following example, the string contains a few spaces and tab characters, so the isspace() method returns True -
str1 = " \t " print("The given string is") print(repr(str1)) print("Checking if the string contains only whitespace characters") print(str1.isspace())
The output obtained is -
The given string is ' \t ' Checking if the string contains only whitespace characters True
Example: Contains Other Characters
In the following example, the string contains spaces and one letter, so the isspace() method returns False -
str2 = " a " print("The given string is") print(repr(str2)) print("Checking if the string contains only whitespace characters") print(str2.isspace())
Following is the output obtained -
The given string is ' a ' Checking if the string contains only whitespace characters False
Using Regular Expressions
You can use the re.fullmatch() function with the pattern \s+ to verify if the entire string contains only whitespace characters. The \s matches any whitespace character.
Example: Regex Whitespace Match
In the following example, the string contains newline and tab characters, which are all valid whitespace characters -
import re str3 = "\n\t\r" print("The given string is") print(repr(str3)) print("Checking if the string contains only whitespace characters") print(bool(re.fullmatch(r"\s+", str3)))
We get the output as shown below -
The given string is '\n\t\r' Checking if the string contains only whitespace characters True
Example: Mixed Content
In the following example, the string includes a digit along with whitespace, so the regular expression does not match fully and returns False -
import re str4 = " 5 " print("The given string is") print(repr(str4)) print("Checking if the string contains only whitespace characters") print(bool(re.fullmatch(r"\s+", str4)))
The result produced is as follows -
The given string is ' 5 ' Checking if the string contains only whitespace characters False
Using Character-wise Check
You can loop through each character and check if it is a whitespace using the isspace() method. If all characters pass the check, the string contains only whitespace.
Example: Manual Check
In the following example, each character in the string is individually checked using the isspace() method -
def is_all_whitespace(s): return all(c.isspace() for c in s) str5 = " \t \n " print("The given string is") print(repr(str5)) print("Checking if the string contains only whitespace characters") print(is_all_whitespace(str5))
The output obtained is -
The given string is ' \t \n ' Checking if the string contains only whitespace characters True
Using the len() Method
The len() method in Python counts and returns the number of elements in the current object.
To check if a string contains only white spaces, first of all, we have to find out the length of the given string using this function. If the length of the string is 0, then it means that the string is empty or contains only whitespace letters; otherwise, the string contains some other characters.
Example
In the example given below, we are checking if a given string is empty or not by using the len() method -
str1 = " " str2 = " DAS" str3 = "" print("Checking whether the given string'",str1,"'is empty") if len(str1) == 0: print('true') else: print('false') print("Checking whether the given string'",str2,"'is empty") if len(str2) == 0: print('true') else: print('false') print("Checking whether the given string'",str3,"'is empty") if len(str3) == 0: print('true') else: print('false')
The output of the above example is -
Checking whether the given string' 'is empty false Checking whether the given string' DAS 'is empty false Checking whether the given string' 'is empty true
Using the strip() Function
Another way to remove spaces is by using the inbuilt strip() method. This method removes all the unnecessary spaces that are present in the string.
To find whether the string contains only empty spaces, we have to compare the stripped string with the original string; if both are the same, then the given string contains only white spaces or the string is empty.
Example
In the program given below, we are checking if a given string contains only spaces or not using the strip() method and comparing it with the actual string -
import re str1 = " " str2 = " DAS" str3 = "" print("Checking whether the given string'",str1,"'contains only spaces") if str1 and str1.strip(): print('false') else: print('true') print("Checking whether the given string'",str2,"'contains only spaces") if str2 and str2.strip(): print('false') else: print('true') print("Checking whether the given string'",str3,"'contains only spaces") if str3 and str3.strip(): print('false') else: print('true')
Output
The output of the above example is -
Checking whether the given string' 'contains only spaces true Checking whether the given string' DAS 'contains only spaces false true