Python Practice › Loop Programs › Program 84
Count uppercase letters in a file
Write a Python program to count the uppercase letters in a file.
Input
file.txt: "Virat Kohli scored 100 in the last match"
Output
2
Approach
- Count characters where
isupper()is True.
Solution
with open("file.txt", "w") as f: f.write("Virat Kohli scored 100 in the last match") with open("file.txt") as f: text = f.read()print(sum(1 for ch in text if ch.isupper()))Output
2
Line by line
Line 5
sum(1 for ...) counts the matching characters.
Learn the concept
New to this topic? Read the lesson, then come back.