In python, how would I go about counting the number of consonants in a word? I realize there are a couple different ways to do this, but I think my way of choice would be to analyze each letter of a word and add to a counter when I encounter a consonant. I just can’t figure out how to implement it.
Something starting with this?
count = 0
consonant = 'astringofconsonants'
if consonant in string[0]:
count += 1
The start you have given is not very pythonic.
Try iterating through the list using
You could also use a generator like the following. It will go through each letter and count the number of each consonant is in the word.
use the
sum()function to add them all up