LambdaLabTM
Computer Science · Class 11 · Strings Revisited
ProgramsCounting⏱️ 15 min read

Counting Programs

The commonest string question in any paper is a counting question, and they are all the same program: a counter above the loop, a test inside it, a print() after. What changes is only the test — so once you can write one of these, you can write all of them.

1Program 1 — count the vowels and the consonants

📋 The problem

Ask for a sentence and report how many vowels and how many consonants it contains. Spaces, digits and punctuation are neither.

Input
what we ask the user for
  • a sentence
Process
what we work out
  • look at each character in turn
  • ignore anything that is not a letter
  • sort the letters into vowel or consonant
Output
what we show
  • the two counts
vowels_consonants.py
# count the vowels and the consonants in a sentence

sentence = input('Enter a sentence: ')
vowels = 0
consonants = 0

for ch in sentence:
    if ch.isalpha():
        if ch.lower() in 'aeiou':
            vowels = vowels + 1
        else:
            consonants = consonants + 1

print('Vowels:', vowels)
print('Consonants:', consonants)
Output
Enter a sentence: LambdaLab teaches Python 3
Vowels: 7
Consonants: 15
if ch.isalpha():

The outer question: is this a letter at all? Without it, the space, the digit and anything else would fall into the else and be counted as consonants — which is the single commonest fault in this program.

if ch.lower() in 'aeiou':

The inner question, asked only of letters. lower() means one test covers 'A' and 'a'; the string 'aeiou' is being used as a list of five things to check against, which is what in does.

vowels = vowels + 1

Twelve spaces of indentation — inside the inner if, inside the outer if, inside the loop. Each level is four, and the level is what decides which question a line belongs to.

Watch Out
Without isalpha(), the count is wrong and looks right. Drop that outer if and the sentence above reports 19 consonants — the 15 real ones plus three spaces and a digit. Nothing errors. The only way to catch it is to count a short sentence by hand and compare.
vowels_consonants.py

2Program 2 — sort every character into a bucket

📋 The problem

Count the capitals, the small letters, the digits, the spaces and everything else in a line of text.

buckets.py
# sort every character of a sentence into one of five buckets

sentence = input('Enter a sentence: ')
upper = 0
lower = 0
digits = 0
spaces = 0
others = 0

for ch in sentence:
    if ch.isupper():
        upper = upper + 1
    elif ch.islower():
        lower = lower + 1
    elif ch.isdigit():
        digits = digits + 1
    elif ch == ' ':
        spaces = spaces + 1
    else:
        others = others + 1

print('Capitals:', upper)
print('Small letters:', lower)
print('Digits:', digits)
print('Spaces:', spaces)
print('Anything else:', others)
Output
Enter a sentence: CBSE Class 11 - Python!
Capitals: 6
Small letters: 9
Digits: 2
Spaces: 4
Anything else: 2
Key Takeaway
It is one if…elif…else ladder, not five separate ifs. Every character belongs in exactly one bucket, and a ladder stops at the first true rung — so nothing can be counted twice. Five separate ifs would work here too, only because the tests happen not to overlap; the ladder says one of these out loud, and the else guarantees nothing is dropped.

Check the total if you want to be sure: 6 + 9 + 2 + 4 + 2 = 23, and len('CBSE Class 11 - Python!') is 23. Every character landed somewhere, which is what the else is for.

3Program 3 — how many times does one character appear?

📋 The problem

Ask for a sentence and a character, and count how often that character occurs — without using count().

tally.py
# count how many times one character appears, without using count()

sentence = input('Enter a sentence: ')
wanted = input('Which character? ')
count = 0

for ch in sentence:
    if ch == wanted:
        count = count + 1

print(wanted, 'appears', count, 'times')
Output
Enter a sentence: mississippi
Which character? s
s appears 4 times
Tip
Python would do this with sentence.count(wanted). Know both. The method is what you would write in real code; the loop is what the question is testing, and it is the one that keeps working when the rule gets fussier — “count the vowels” has no method, and neither has “count the capitals that come after a space”.

4Program 4 — count the words

📋 The problem

Count the words in a sentence. Two ways: by counting the gaps, and by letting Python do the splitting.

word_count.py
# count the words by counting the gaps between them

sentence = 'the quick brown fox jumps'
words = 1

for ch in sentence:
    if ch == ' ':
        words = words + 1

print('Words:', words)
print('Words, using split():', len(sentence.split()))
Output
Words: 5
Words, using split(): 5

Five words have four gaps between them, which is why the counter starts at 1 and not 0 — it is counting the gaps and then counting the last word separately, in advance.

Watch Out
The gap-counting version is fragile, and worth knowing why. Two spaces between words count as two gaps and give one word too many. A space at the end does the same. An empty string reports one word when there are none. split() has none of those problems, because it splits on runs of whitespace and drops the empties — which is exactly the sort of detail a hand-written loop is expected to get wrong.
word_count.py — the same program on 'the quick fox ' (two spaces, trailing space)
Output
Words: 5
Words, using split(): 3

5Program 5 — how many of each character?

📋 The problem

Report how many times every character appears, not just one.

One counter per character is impossible to write in advance — you do not know which characters will turn up. A dictionary is the answer: the character is the key, the tally is the value, and keys are created as they are met.

frequency.py
# how many times does each character appear?

word = 'banana'
counts = {}

for ch in word:
    counts[ch] = counts.get(ch, 0) + 1

print(counts)
Output
{'b': 1, 'a': 3, 'n': 2}
counts = {}

An empty dictionary, made before the loop — the same 'collector above the loop' idea, with a dictionary instead of a number.

counts[ch] = counts.get(ch, 0) + 1

get(ch, 0) asks for the tally so far and answers 0 when the character has never been seen. Writing counts[ch] with a key that does not exist yet creates it, so the first sighting stores 1.

Watch Out
counts[ch] + 1 would raise KeyError. The first time a character turns up it has no entry, and asking for a key that is not there is an error, not a zero. That is the whole reason get() is here, and it is why the dictionary lesson makes such a point of the difference between d[k] and d.get(k, 0).

The keys come out in the order the characters were first seen — b, then a, then n — because that is the order Python keeps a dictionary in. Not alphabetical, and not by size.

6Recap

One counter per thing being counted

Started above the loop, raised inside it, printed after. Five buckets means five names, all set to 0 before the loop begins.

Filter before you classify

isalpha() first, then vowel-or-consonant. Skip the filter and every space and digit falls into the else.

A ladder counts each character once

if…elif…else stops at the first true rung, and the else catches whatever the earlier tests did not.

A dictionary counts what you cannot name in advance

counts.get(ch, 0) + 1 — the key is the character, and get() supplies the 0 that a missing key would otherwise refuse.

✍️ Now write these yourself
  1. 1

    Count the digits in a sentence and add them up at the same time.

    Hint · Two collectors, one if ch.isdigit(), and int(ch) for the total.

  2. 2

    Count how many words in a sentence begin with a capital letter.

    Hint · Loop over sentence.split() and test w[0].isupper().

  3. 3

    Count the vowels, but stop counting as soon as you have found five.

    Hint · A break inside the if, once the counter reaches 5.

  4. 4

    Count how many characters of a sentence are not letters, digits or spaces.

    Hint · The five-bucket program with three buckets thrown away — or one if with three anded nots.

  5. 5

    Report which vowel appears most often in a word, using the frequency dictionary.

    Hint · Build the dictionary first, then loop over 'aeiou' and compare counts.get(v, 0).

Quick Check

A vowel/consonant counter drops the if ch.isalpha() test. What happens to 'Hi 7!'?

Quick Check

Why does counts[ch] = counts.get(ch, 0) + 1 use get() rather than counts[ch] + 1?

Quick Check

The gap-counting word counter is given 'the quick fox ' — two spaces in the middle and one at the end. What does it report?