6. Count the vowels
Write a function count_vowels(text) that takes a string text and returns how many vowels it contains, as a whole
number.
The vowels are a, e, i, o and u, in lower case or as capitals (A, E, I, O, U). Count each vowel
every time it appears. Nothing else counts: not y, and not spaces, digits or punctuation. If text is empty, or
has no vowels, return 0.
Example 1
text = "Hello World"3Example 2
text = "Python"1Example 3
text = "APPLE pie"4Constraints
texthas 0 to 100 characters.- It holds English letters, digits, spaces and punctuation.
Submit also runs 4 more tests you can't see.
Practises Strings (lesson 4).
- Go through the text one character at a time with
for ch in text:, keeping a count that starts at 0. ch.lower()turns a capital into lower case, soAandalook the same and you only check one case of each vowel.ch.lower() in "aeiou"isTruefor a vowel: add 1 to the count then.y, spaces, digits and punctuation are never in"aeiou".
Solve it without hints for 3 stars, with hints for 2, after seeing the solution for 1.
One way to solve it, once you've had a go.
Solving it after seeing the solution earns 1 star.
Your Submits show here, with their code, so you can go back to one. They stay in this browser.
def count_vowels(text):
"""Count the vowels in a string.
text: str, any characters; may be empty
Returns: int, how many characters are a, e, i, o or u,
in lower case or as capitals
"""
# Write your code here
pass
Run your code to see how it does on the test cases, or Submit to check every test.