- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 text is “empty” (spaces, tabs, newlines) in Python?
In this article, we are going to focus on how to check if the text is empty (spaces tabs newlines) in Python.
The first method is to use the built-in string library's isspace() method. The isspace() method determines whether a string contains only spaces or contains any other characters.
If the given string is only made up of spaces, this method returns true; otherwise, it returns false. Even if the string contains characters like t and n, the method returns true.
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 as shown below −
Checking whether the given string' 'contains only spaces True Checking whether the given string' DAS 'contains only spaces False Checking whether the given string' 'contains only spaces True
Using Regular Expressions
The second method involves the use of Regular expressions. The search method of the re library uses the regular expression "s*$," which returns true if the string contains only spaces and false if the string contains any other characters.
Example 1
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 given below−
Checking whether the given string' 'contains only spaces true Checking whether the given string' DAS 'contains only spaces false Checking whether the given string' 'contains only spaces true
Example 2
For matching only whitespace, we can also call the re.match(regex, string) using the regex metacharacter \s as follows: "^\s*$" −
import re print(bool(re.match('^\s+$', ' abc'))) print(bool(re.match('^\s+$', ' ')))
Output
Following is an output of the above code −
False True
Using len() function
The third option is to make use of the built−in len() function. The length of the given string must be determined. If the string's length is 0, it means the string is empty or only contains whitespace letters; otherwise, the string contains 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 given below −
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 strip() function
The fourth option is to make use of the built-in strip() function. The strip() function removes all of the unnecessary spaces from the string. To determine whether a string contains only empty spaces, compare the stripped string to the original string; if they are identical, the string contains only white spaces or 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 as shown below −
Checking whether the given string' 'contains only spaces true Checking whether the given string' DAS 'contains only spaces false Checking whether the given string' 'contains only spaces true