Python Practice › File Handling Programs › Program 29
Count digits in a file
Write a Python program to count the digits in a file.
Output
12
Approach
sum(ch.isdigit() for ch in text).
Solution
with open("story.txt", "w") as f: f.write("Order 4521 shipped on 2026-11-02\n") with open("story.txt") as f: print(sum(ch.isdigit() for ch in f.read()))Output
12
Learn the concept
New to this topic? Read the lesson, then come back.