- 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
Python program to count the number of blank spaces in a text file
Blank spaces in a text file refer to the empty spaces or gaps between words, sentences, or characters. These spaces are typically represented by the space character (' ') or other whitespace characters, including tab ('\t'), newline ('\n'), carriage return ('\r'), form feed ('\f'), and vertical tab ('\v'). These characters, as per the Unicode standard, represent empty space or formatting elements in the text.
When counting the number of blank spaces in a text file, we are essentially looking for these whitespace characters to determine the frequency or distribution of spaces within the text. This information can be useful for various purposes, such as text analysis, data cleaning, or formatting adjustments.
In this article, we will see different approaches to counting the number of blank spaces in a text file using Python programming.
Using the count() method
The count() method is a built-in method in Python that is available for string objects. It allows us to count the number of occurrences of a specified substring within a string.
Following is the syntax of the count() method −
string.count(substring, start, end)
Where,
string − This is the original string on which we want to perform the counting
substring − This is the substring that we want to count within the original string.
start (optional) − This is the starting index from where the search for the substring should begin. By default, it starts from index 0.
end (optional) − This is the ending index where the search for the substring should stop. By default, it searches until the end of the string.
The method returns an integer value representing the number of occurrences of the specified substring within the original string.
Example
In this example, we will use the count() method to count the number of blank spaces in a text file.
def count_blank_spaces(file_path): count = 0 with open(file_path, 'r') as file: for line in file: count += line.count(' ') return count # defining the text file path file_path = 'sample_document.txt' blank_space_count = count_blank_spaces(file_path) print(f"Number of blank spaces in the file: {blank_space_count}")
Output
Number of blank spaces in the file: 34
Using the isspace() method
In the context of counting blank spaces in a text file, the isspace() method can be used to identify and count individual whitespace characters (such as spaces) within each line of the file.
The isspace() method is a built-in string method in Python that checks whether a string consists of whitespace characters. It returns True if all the characters in the string are whitespace characters, and False otherwise.
Example
Here's an example that counts the number of blank spaces in a text file using the isspace() method.
def count_blank_spaces(file_path): count = 0 with open(file_path, 'r') as file: for line in file: count += sum(1 for char in line if char.isspace()) return count # defining the text file path file_path = 'sample_document.txt' blank_space_count = count_blank_spaces(file_path) print(f"Number of blank spaces in the file: {blank_space_count}")
Output
Number of blank spaces in the file: 34
Using the re.findall() function
In this approach, we will utilize the re.findall() function from the re module to find all occurrences of whitespace characters in the text file. The regular expression “\s” matches any whitespace character, including spaces, tabs, and newlines.
Example
Let’s see the below example to count the number of blank spaces in a text file using the re.findall() method.
import re def count_blank_spaces(file_path): with open(file_path, 'r') as file: text = file.read() count = len(re.findall(r'\s', text)) return count # defining the text file path file_path = 'sample_document.txt' blank_space_count = count_blank_spaces(file_path) print(f"Number of blank spaces in the file: {blank_space_count}")
Output
Number of blank spaces in the file: 34
Above are the different approaches to calculating the total number of Whitespaces in a text file.