Python Practice › Loop Programs › Program 84

Count uppercase letters in a file

EasyLoop Programs · 84 of 90

Write a Python program to count the uppercase letters in a file.

Advertisement
Input
file.txt: "Virat Kohli scored 100 in the last match"
Output
2

Approach

  1. Count characters where isupper() is True.

Solution

Python
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.

More loop programs

← All 90 Loop Programs

Leave a Comment