How to check if a string contains only whitespace letters in Python?


A string is a group of characters that can represent a single word or a complete sentence. In Python there is no need to declare variables explicitly using the datatype specifier you can directly assign them to a literal. Therefore, compared to other technologies like Java it is easy to use Python strings.

For manipulating and accessing strings, Python includes a number of built in functions and methods. A string is an object of the String class, which has multiple methods because everything in Python is an object.

In this article, we are going to find out how to check if a string contains only whitespace letters in Python

Using the isspace() method

One way to achieve this is by using the isspace() method of the inbuilt string library. This method tells us whether the string contains only spaces or if any other character is present. This method returns true if the given string is only made up of spaces otherwise it returns false. The method returns true even if the string is made up of characters like \t, \n.

Example

In the program given below, we are taking 3 different strings and finding out if they contain only spaces using the isspace() method.

str1 = " " str2 = " DAS" str3 = "\n\t" print("Checking whether the given string'",str1,"'contains only spaces") print(str1.isspace()) print("Checking whether the given string'",str2,"'contains only spaces") print(str2.isspace()) print("Checking whether the given string'",str3,"'contains only spaces") print(str3.isspace())

Output

The output to the above given example is,

("Checking whether the given string'", ' ', "     'contains only spaces")
True
("Checking whether the given string'", ' DAS', "'contains only spaces")
False
("Checking whether the given string'", '\n\t', "'contains only spaces")
True

Using regular expressions

You can also check if a given sting is a white space using Regular Expressions. The regular expression “^\s*$” is used in the search method of re library and it returns true if the string contains only spaces and it returns false if the string contains any other characters.

Example

In the program given below, we are taking 3 different strings and finding out if they contain only spaces using the Regular expression “^\s*$” and search method of re library.

import re str1 = " " str2 = " DAS" str3 = "\n\t" print("Checking whether the given string'",str1,"'contains only spaces") if not str1 or re.search("^\s*$", str1): print('true') else: print('false') print("Checking whether the given string'",str2,"'contains only spaces") if not str2 or re.search("^\s*$", str2): print('true') else: print('false') print("Checking whether the given string'",str3,"'contains only spaces") if not str3 or re.search("^\s*$", str3): print('true') else: print('false')

Output

The output of the above example is,

("Checking whether the given string'", ' ', "     'contains only spaces")
true
false
("Checking whether the given string'", '\n\t', "'contains only spaces")
true

Using the len() function

The len() function in Python counts and returns the number of elements in the current object. 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')

Output

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() function. This function 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

Updated on: 19-Oct-2022

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements