Count Number of Characters in Text File - Python Examples
Python - Count Number of Characters
You can count the number of characters in a text file by first reading the text to a variable and then counting the characters. This tutorial will guide you through the sequence of steps required to achieve this.
Steps to Count Number of Characters
To count the number of characters in a text file, follow these steps:
- Open the file in read mode.
- Read the text using the
read()
function. - Get the length of the string using the
len()
function, which gives the number of characters in the text file. - You can refine the count by cleaning the string, such as removing white space characters or punctuation marks, depending on your needs.
Examples
1. Count Characters in a Given Text File
In this Python Example, we will read a text file and count the total number of characters in it. Consider the following text file:
Text File
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
Python Program
# Open file in read mode
file = open("C:\data.txt", "r")
# Read the content of the file
data = file.read()
# Get the length of the data
number_of_characters = len(data)
print('Number of characters in text file:', number_of_characters)
Explanation (Step-by-Step):
open("C:\data.txt", "r")
: Opens the file in read mode.file.read()
: Reads the entire content of the file into a variable calleddata
.len(data)
: Calculates the total number of characters in the stringdata
.- The result is printed to the console.
Output
Number of characters in text file: 97
2. Count Characters in a Text File Excluding Spaces
In this Python Example, we will count the number of characters in a text file, excluding white space characters. Consider the following text file:
Text File
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
Python Program
# Open file in read mode
file = open("C:\data.txt", "r")
# Read the content of the file and remove spaces
data = file.read().replace(" ", "")
# Get the length of the data
number_of_characters = len(data)
print('Number of characters in text file (excluding spaces):', number_of_characters)
Explanation (Step-by-Step):
file.read()
: Reads the file's content intodata
.replace(" ", "")
: Removes all spaces from the string.len(data)
: Counts the remaining characters after removing spaces.- The result is printed to the console.
Output
Number of characters in text file (excluding spaces): 84
3. Count Characters Excluding Special Characters
In this example, we count the number of characters in a text file while excluding both white spaces and special characters.
Text File
Welcome to pythonexamples.org! Here, you'll find: python programs, tips, and tricks.
Python Program
import re
# Open file in read mode
file = open("C:\data.txt", "r")
# Read the content of the file
data = file.read()
# Remove spaces and special characters
cleaned_data = re.sub(r'[^a-zA-Z0-9]', '', data)
# Get the length of the cleaned data
number_of_characters = len(cleaned_data)
print('Number of characters in text file (excluding spaces and special characters):', number_of_characters)
Explanation (Step-by-Step):
file.read()
: Reads the file's content intodata
.re.sub(r'[^a-zA-Z0-9]', '', data)
: Uses a regular expression to remove all characters that are not letters or numbers.len(cleaned_data)
: Counts the remaining alphanumeric characters.- The result is printed to the console.
Output
Number of characters in text file (excluding spaces and special characters): 63
Summary
In this tutorial of Python Examples, we learned how to count the number of characters in a text file using Python. We explored examples for counting all characters, excluding spaces, and excluding both spaces and special characters. These examples demonstrate how to manipulate strings and refine your character count as needed.