Count the Number of Characters in a File in Python
To count the number of characters in a file in Python, we can use the open()
function to read the file content and then apply the len()
function to determine the total number of characters. This method efficiently calculates the character count, including spaces, newline characters, and special symbols.
Examples
1. Counting Characters in a File Using len()
In this example, we open a file sample.txt in read mode and count the number of characters using the len()
function.
sample.txt
The old man found an old book in his old library.
Learning Python is fun.
An error in the code caused a major error during execution.
main.py
# Open the file in read mode
with open("sample.txt", "r") as file:
content = file.read() # Read the file content
char_count = len(content) # Count the number of characters
# Print the character count
print("Total number of characters:", char_count)
Explanation
Here’s how we are achieving the character count:
- We use
open("sample.txt", "r")
to open the file in read mode. - The
read()
method reads the entire content of the file into the variablecontent
. - The
len(content)
function calculates the total number of characters, including spaces and newline characters. Reference: len() built-in function. - Finally, we print the character count.
Output

2. Counting Characters Without Spaces and Newlines
To count only meaningful characters, we remove spaces and newline characters before counting.
sample.txt
The old man found an old book in his old library.
Learning Python is fun.
An error in the code caused a major error during execution.
main.py
# Open the file in read mode
with open("sample.txt", "r") as file:
content = file.read() # Read the file content
char_count = len(content.replace(" ", "").replace("\n", "")) # Remove spaces and newlines
# Print the character count
print("Total number of characters (excluding spaces & newlines):", char_count)
Explanation
In this example, we modify the character counting logic:
- We read the file content using
file.read()
. - We use
content.replace(" ", "").replace("\n", "")
to remove all spaces and newline characters. - The
len()
function then counts only meaningful characters. - Finally, we print the refined character count.
Output

3. Counting Only Alphabetic Characters
In this example, we count only letters (ignoring numbers, punctuation, and spaces) in the sample.txt file.
sample.txt
Hello World
1234567890
main.py
# Open the file in read mode
with open("sample.txt", "r") as file:
content = file.read() # Read file content
char_count = sum(c.isalpha() for c in content) # Count only alphabetic characters
# Print the character count
print("Total number of alphabetic characters:", char_count)
Explanation
We refine our approach further to count only alphabetic characters:
- We read the file using
file.read()
. - The expression
sum(c.isalpha() for c in content)
iterates through the file content and counts only letters. References: string.isalpha(), For loop, sum() - Non-alphabetic characters (numbers, punctuation, spaces) are excluded from the count.
- The final count is printed.
Output

Conclusion
In this tutorial, we covered different methods to count characters in a file:
- Using
len()
to count all characters. - Excluding spaces and newline characters for a refined count.
- Counting only alphabetic characters for text-based analysis.