Solution: Count the Number of Vowels in a String
Learn how to write a recursive function to count the number of vowels in a string in Python.
We'll cover the following...
The solution to the problem of counting the number of vowels in a string is given below.
Solution
Press + to interact
def counts_vowels(s, idx, count) :if idx == len(s):return countif s[idx] == 'a' or s[idx] == 'e' or s[idx] == 'i' or s[idx] == 'o' or s[idx] == 'u' :count += 1count = counts_vowels(s, idx + 1, count)return countcount = counts_vowels('Raindrops on roses', 0, 0)print(count)
Explanation
- Lines