Easy ยท ages 10+ ยท strings ยท for loop ยท in
Vowel counter in Python.
Count the vowels in a sentence.
The program
Press Run to see what it prints. Change anything you like, then run it again.
sentence = "Learning to code is a lot of fun"
vowels = "aeiou"
count = 0
for letter in sentence.lower():
if letter in vowels:
count = count + 1
print("Vowels:", count)
print("Letters:", len(sentence.replace(" ", "")))Vowels: 11 Letters: 25
How it works
- A
forloop can go through a string one letter at a time. letter in vowelsisTruewhen the letter appears in the string of vowels.replace(" ", "")removes spaces so thatlencounts only letters.
Try this
- Count each vowel separately.
- Count the words with
sentence.split(). - Find the longest word.
Stuck on an error? Read how to debug Python.
Similar ideas
More programs
FAQ
Vowel counter: questions
What does the vowel counter program do?
Count the vowels in a sentence. It prints: Vowels: 11 / Letters: 25
Which Python ideas does it use?
strings, for loop, in. Easy level, suitable from about age 10.
How do I run it?
Press Run on this page. To run it on your own computer, install Python 3 and save the code in a file ending in .py. It prints the same thing.
What should I try next?
Count each vowel separately. Count the words with sentence.split(). Find the longest word.
Start a conversation
Want a teacher to guide the learning?
Tell us the learner's age and what they can already do, and we will suggest a starting point. A parent or guardian can make the enquiry.
- Tell us the learnerโs age or grade and goals
- Weโll suggest a suitable class or learning path
- Weโll confirm format, timing and fees with a parent or guardian
Play it, don't just read it
Write some code right now
Free games that need no sign-up. Pick one and play for five minutes.
Pick a game
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay