Count Vowels in a String

Write a Python function that takes a string as input and counts and returns the number of vowels in the string. Consider ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ as vowels.

Example 1:

Input: "hello world" 
Output: 3

Example 2:

Input: "sky" 
Output: 0

Use a loop to iterate through the characters in the string.

def count_vowels(s):
    return sum(1 for char in s if char.lower() in 'aeiou')

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!