+ 1
Help me with this python problem ??
Assume s is a string of lower case characters. Write a program that counts up the number of vowels contained in the string s. Valid vowels are: 'a', 'e', 'i', 'o', and 'u'. For example, if s = 'azcbobobegghakl', your program should print: Number of vowels: 5
3 Answers
+ 4
I don't want to give you the answer directly, so I'll just suggest some things that you can do. What I would do when making a program like this would be to create a loop that goes through the string, held in a variable, with an if statement within the loop to check if the value that loop is currently at is equal to a vowel.
+ 3
Using list comprehension you can do this in one line like so:
len([l for l in s if l in 'aeiouAEIOU'])
where s is the string you wish to check. This would be the same as creating an empty list and then looping over the string checking each letter if it is a vowel and then adding the letter to the list. When done you check the length of the list to get the number of vowels in that string.
The long form would look like:
c=[]
for l in s:
if l in 'aeiouAEIOU':
c.append(l)
len(c)
+ 1
use any loop
check if s[index] is equal to vowels
if yes add 1 to some variable