Counting vowels and consonants in a string
up vote
3
down vote
favorite
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 countn")
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)
python python-3.x strings
add a comment |
up vote
3
down vote
favorite
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 countn")
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)
python python-3.x strings
I've gone and corrected the sample output by changing 'upper case' F to become 'lower case' f. Thanks! ;-)
– Paul Ramnora
Dec 4 at 23:39
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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 countn")
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)
python python-3.x strings
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 countn")
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)
python python-3.x strings
python python-3.x strings
edited Dec 4 at 23:39
200_success
128k15149412
128k15149412
asked Dec 4 at 22:56
Paul Ramnora
163
163
I've gone and corrected the sample output by changing 'upper case' F to become 'lower case' f. Thanks! ;-)
– Paul Ramnora
Dec 4 at 23:39
add a comment |
I've gone and corrected the sample output by changing 'upper case' F to become 'lower case' f. Thanks! ;-)
– Paul Ramnora
Dec 4 at 23:39
I've gone and corrected the sample output by changing 'upper case' F to become 'lower case' f. Thanks! ;-)
– Paul Ramnora
Dec 4 at 23:39
I've gone and corrected the sample output by changing 'upper case' F to become 'lower case' f. Thanks! ;-)
– Paul Ramnora
Dec 4 at 23:39
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
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 countn")
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])
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209034%2fcounting-vowels-and-consonants-in-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
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 countn")
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])
add a comment |
up vote
3
down vote
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 countn")
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])
add a comment |
up vote
3
down vote
up vote
3
down vote
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 countn")
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])
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 countn")
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])
answered Dec 5 at 0:05
200_success
128k15149412
128k15149412
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209034%2fcounting-vowels-and-consonants-in-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I've gone and corrected the sample output by changing 'upper case' F to become 'lower case' f. Thanks! ;-)
– Paul Ramnora
Dec 4 at 23:39