Goal: Visit each character of a string and count the vowels.
A string is a row of letters
Every message you type, every name in your contacts, is a string: a row of characters, one per box, numbered from 0 just like an array. "cat" has 3 boxes: c in box 0, a in box 1, t in box 2.
A space is a character too, and it takes a box. So "hi there" has 8 characters, not 7. Punctuation and digits count the same way.
- count
- 0
Box 0 holds 's'. Not a vowel. Just like an array, we look at one box at a time.
Everything from arrays still works
Walking a string is walking an array: visit each box from the first to the last, and do something with the character you find. Counting vowels is the counting loop you already know, with a new question at each box: is this character one of a, e, i, o, u?
An empty string has no boxes, so the loop doesn't run and the count stays 0.
count = 0
for ch in s:
if ch == " ":
count += 1