One way to solve it is to use a comprehensilist on (much more succinct and pythonic):
vogais = 'aeiou'
s = 'abcdefghijABCEx'
qtd_vogais = len([c for c in s.lower() if c in vogais])
print(qtd_vogais) # 5
The line qtd_vogais = len([c for c in s.lower() if c in vogais])
is equivalent to loop suggested by the other answers:
vogais = 'aeiou'
s = 'abcdefghijABCEx'
qtd_vogais = 0
for c in s.lower():
if c in vogais:
qtd_vogais += 1
print(qtd_vogais) # 5
Basically, s.lower()
turns the string into lower-case letters, and for c in s.lower()
makes a loop for all characters in the string. At each iteration, c
will be a character. Then just test if c
is a vowel and increment the counter.
In the case of comprehensilist on, a list with all vowels (because the expression is inside brackets), and the function len
returns the size of this list.
If you also want to consider accented characters, you can use the module unicodedata
:
from unicodedata import normalize
vogais = 'aeiou'
s = 'ábcdefghijABCEx'
qtd_vogais = len([c for c in normalize('NFD', s.lower()) if c in vogais])
print(qtd_vogais) # 5
In a nutshell, the normalization in NFD "breaks" the "á" in two characters: the a
(without accent) and the accent itself. Thus, it is possible to check the vowels in the same way as before. (has a more detailed explanation of the standardisation here, here and here - although not in Python, the idea is the same)
Another option is to use a Counter
:
from collections import Counter
from unicodedata import normalize
vogais = 'aeiou'
s = 'ábcdefghijABCEx'
c = Counter(normalize('NFD', s.lower()))
qtd_vogais = sum(qtd for letra, qtd in c.items() if letra in vogais)
print(qtd_vogais) # 5
The Counter
resulting is a dictionary, whose keys are string characters and values are the amount of times each character occurs in the string.
Then just go through it, see which keys are vowels and add up the amounts.
And what’s the mistake Alex? Apparently there doesn’t seem to be anything wrong. You’re just not printing
j
, but seems to be counting normally.– fernandosavio
Jeez fuck, I’m forehead here.
– user141036
Alex,
string[i]
doesn’t make sense, becausei
will be every letter of the string. So it’s easier to simply use the operatorin
. Kind of:if i.lower() in "aeiou":
– fernandosavio
It worked very well no, apparently does not present the expected number of vowels, I will edit my code.
– user141036
I will create an answer with the explanation. Only ready-made code will not help you understand the concept.
– fernandosavio
It will not be necessary already posted the reply you wanted and now I understood what was wrong, thanks anyway.
– user141036
Fernando, excellent, post as it may help another person who by venture goes through the same problem. :)
– Filipe L. Constante
Okay, if you already know what the
str.lower()
and already knows the operatorin
all right...– fernandosavio
All right, Filipe.. I’ll post it the same way then...
– fernandosavio
Tá @fernandosavio.
– user141036
Posted... Any questions just comment on the reply...
– fernandosavio