S2Q2 · Vowel Count of Words¶
⚡ Quick Reference
Type: Full I/O problem
Core idea: split the sentence into words, count vowels in each word, append the count in parentheses, rejoin with spaces.
s = input()
vowels = "aeiouAEIOU"
words = s.split(" ")
result = [f"{w}({sum(1 for c in w if c in vowels)})" for w in words]
print(" ".join(result))
Key rules:
- Both lowercase and uppercase vowels count
- Split on " " (single space), not split() - to preserve spacing behaviour
- Output format: word(count) with no space before (
- Rejoin with a single space between words
Problem Statement¶
Problem (I/O type)
Read a string, count the vowels in each word, and print each word followed immediately by its vowel count in parentheses. Words are separated by spaces; spaces must be retained in the output.
Example:
Computer science is amazing
Computer(3) science(3) is(1) amazing(3)
Understanding the problem¶
Two things to handle per word:
- Count vowels - how many characters in the word are in
"aeiouAEIOU" - Format output - append the count immediately in parentheses:
word(count)
Then join all formatted words back with spaces.
Counting vowels in a word
Walk through each character - if it's a vowel, count it.sum(1 for ...) is the idiomatic counter pattern.
Tracing the example¶
Input: "Computer science is amazing"
| Word | Vowels found | Count |
|---|---|---|
Computer |
o, u, e |
3 |
science |
i, e, e |
3 |
is |
i |
1 |
amazing |
a, i, a |
3 |
Output: Computer(3) science(3) is(1) amazing(3)
Solution approaches¶
s = input()
vowels = "aeiouAEIOU"
print(" ".join(f"{w}({sum(1 for c in w if c in vowels)})" for w in s.split(" ")))
Everything in one print call - split, count, format, and join in a single generator expression.
s = input()
vowels = "aeiouAEIOU"
words = s.split(" ")
result = []
for word in words:
count = 0
for c in word:
if c in vowels:
count += 1
result.append(f"{word}({count})")
print(" ".join(result))
Each step named and visible - reading the input, counting vowels per word, building the result list, and printing.
Why split(" ") and not split()?¶
split() with no argument splits on any whitespace and ignores leading/trailing spaces and multiple consecutive spaces. split(" ") splits on exactly one space and preserves empty strings between consecutive spaces.
For this problem either works since the input is a simple sentence with single spaces. However split(" ") is safer when the problem says "spaces should be retained" - it makes the intent explicit.
Key takeaways¶
sum(1 for c in w if c in vowels)
The standard pattern for counting characters matching a condition. Replace vowels with any set of target characters and it generalises to any character-counting problem.
f"{w}({count})" for formatting
f-strings handle the word(count) format cleanly. The count is computed inline - no need for a separate variable if using the Pythonic approach.
" ".join(list) to reassemble
After transforming each word into a string, " ".join(result) puts them back together with single spaces - exactly matching the expected output format.