Count Vowels in String Python

Count Vowels in String Python

What you want can be done quite simply like so:

>>> mystr = input(Please type a sentence: )
Please type a sentence: abcdE
>>> print(*map(mystr.lower().count, aeiou))
1 1 0 0 0
>>>

In case you dont know them, here is a reference on map and one on the *.

def countvowels(string):
    num_vowels=0
    for char in string:
        if char in aeiouAEIOU:
           num_vowels = num_vowels+1
    return num_vowels

(remember the spacing s)

Count Vowels in String Python

>>> sentence = input(Sentence: )
Sentence: this is a sentence
>>> counts = {i:0 for i in aeiouAEIOU}
>>> for char in sentence:
...   if char in counts:
...     counts[char] += 1
... 
>>> for k,v in counts.items():
...   print(k, v)
... 
a 1
e 3
u 0
U 0
O 0
i 2
E 0
o 0
A 0
I 0

Leave a Reply

Your email address will not be published. Required fields are marked *