3
\$\begingroup\$

The output should display as follows...

PROGRAM: Vowel count

Please, enter a text string: fred
4 characters
a e i o u
0 1 0 0 0
b c d f g h j k l m n p q r s t v w x y z
0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 

It works; but, I believe is way over long...; maybe, I should have used arrays/or, ASCII count/-etc.?

vowels="aeiou"
aCount=eCount=iCount=oCount=uCount=0

consonants="bcdfghjklmnpqrstvwxyz"
bCount=cCount=dCount=fCount=gCount=hCount=jCount=kCount=lCount=mCount=nCount=oCount=pCount=qCount=rCount=sCount=tCount=uCount=vCount=wCount=xCount=yCount=zCount=0

print("PROGRAM: Vowel count\n")

aString=input("Please, enter a text string: ")

for eachChar in aString:

    if eachChar in vowels:
        if eachChar == "a": aCount+=1
        if eachChar == "e": eCount+=1
        if eachChar == "i": iCount+=1
        if eachChar == "o": oCount+=1
        if eachChar == "u": uCount+=1

    if eachChar in consonants:
        if eachChar == "b": bCount+=1
        if eachChar == "c": cCount+=1
        if eachChar == "d": dCount+=1
        if eachChar == "f": fCount+=1
        if eachChar == "g": gCount+=1        
        if eachChar == "h": hCount+=1
        if eachChar == "j": jCount+=1
        if eachChar == "k": kCount+=1
        if eachChar == "l": lCount+=1
        if eachChar == "m": mCount+=1
        if eachChar == "n": nCount+=1
        if eachChar == "p": pCount+=1
        if eachChar == "q": qCount+=1
        if eachChar == "r": rCount+=1
        if eachChar == "s": sCount+=1
        if eachChar == "t": tCount+=1
        if eachChar == "v": vCount+=1
        if eachChar == "w": wCount+=1
        if eachChar == "x": xCount+=1        
        if eachChar == "y": yCount+=1
        if eachChar == "z": zCount+=1

print(len(aString),"characters")

print("a","e","i","o","u")
print(aCount,eCount,iCount,oCount,uCount)

print("b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z")
print(bCount,cCount,dCount,fCount,gCount,hCount,jCount,kCount,lCount,mCount,nCount,pCount,qCount,rCount,sCount,tCount,vCount,wCount,xCount,yCount,zCount)
\$\endgroup\$
1
  • \$\begingroup\$ I've gone and corrected the sample output by changing 'upper case' F to become 'lower case' f. Thanks! ;-) \$\endgroup\$ Dec 4, 2018 at 23:39

1 Answer 1

3
\$\begingroup\$

To count the occurrences of things, use collections.Counter.

The lowercase letters are available as a predefined constant string.ascii_lowercase. You can use a generator expression to filter out the vowels and obtain the consonants. PEP 8, the official style guide, suggests using ALL_CAPS as names for constants.

I've used the * operator when calling print() to treat each element of a tuple or list as a separate argument.

Note that your formatting will break when any character has more than 9 occurrences.

from collections import Counter
from string import ascii_lowercase

VOWELS = tuple("aeiou")
CONSONANTS = tuple(c for c in ascii_lowercase if c not in VOWELS)

print("PROGRAM: Vowel count\n")
s = input("Please, enter a text string: ")
counts = Counter(s)

print('{0} characters'.format(len(s)))
print(*VOWELS)
print(*[counts[c] for c in VOWELS])
print(*CONSONANTS)
print(*[counts[c] for c in CONSONANTS])
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.