Editor

Python Program to Count Vowels, Consonants, Uppercase and Lowercase Characters in a Text File

Beginner · File handling · CBSE class 12 practical list

This program reads a text file and counts four things: vowels (a, e, i, o, u), consonants (all the other letters), uppercase letters and lowercase letters. Spaces, digits and punctuation are not letters, so they are skipped. It is one of the standard file handling programs in the CBSE class 12 practical list.

Program

count_vowels_in_file.py
# Make a sample file to work on (in your practical file, this file already exists)
with open("story.txt", "w") as f:
f.write("Python Is Fun.\n")
f.write("CBSE Class 12 Practical File\n")
vowels = 0
consonants = 0
uppercase = 0
lowercase = 0
with open("story.txt", "r") as f:
text = f.read() # the whole file as one string
for ch in text:
if ch.isalpha(): # only letters are counted
if ch.lower() in "aeiou":
vowels += 1
else:
consonants += 1
if ch.isupper():
uppercase += 1
else:
lowercase += 1
print("Vowels:", vowels)
print("Consonants:", consonants)
print("Uppercase letters:", uppercase)
print("Lowercase letters:", lowercase)
Output
Vowels: 10
Consonants: 23
Uppercase letters: 10
Lowercase letters: 23

How it works

  1. The program writes two lines to story.txt first, then reads the whole file into text with f.read().
  2. The loop looks at one character at a time, and ch.isalpha() lets only letters through, so the digits in 12, the spaces, the full stop and the newlines are ignored.
  3. ch.lower() in "aeiou" catches both small and capital vowels, and every other letter is a consonant.
  4. Separately, ch.isupper() decides whether the letter is uppercase or lowercase, so each letter is counted twice: once as a vowel or consonant, and once by its case.
  5. This sample file has 33 letters: 10 vowels and 23 consonants, and also 10 uppercase and 23 lowercase letters.

Another way to write it

Counting digits, spaces and other characters too

Counting digits, spaces and other characters too
# Make a sample file to work on (in your practical file, this file already exists)
with open("story.txt", "w") as f:
f.write("Room 42, Block B!\n")
digits = 0
spaces = 0
others = 0
with open("story.txt", "r") as f:
for ch in f.read():
if ch.isdigit():
digits += 1
elif ch == " ":
spaces += 1
elif not ch.isalpha() and ch != "\n":
others += 1
print("Digits:", digits)
print("Spaces:", spaces)
print("Other characters:", others)
Output
Digits: 2
Spaces: 3
Other characters: 2

Some versions of this question also ask for digits, spaces and special characters. isdigit() and a check for " " handle the first two, and the newline at the end of the line is left out on purpose.

Try it yourself

Add a fifth counter for digits, so the 1 and the 2 in "Class 12" are counted too.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises