| | |
Python Code Help - Capitalize only vowels
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 5
Reputation:
Solved Threads: 0
I am a beginner python programmer and need some help with this problem.
To provide a code that will lowercase all consonants and upper case all vowels. Needs to be a relatively simple code (like 2-3 lines lol)
For example i have started this:
S=raw_input("Enter Sentence: ")
print S.upper() # This will capitalize all
print S.lower() # This will lowercase all
To provide a code that will lowercase all consonants and upper case all vowels. Needs to be a relatively simple code (like 2-3 lines lol)
For example i have started this:
S=raw_input("Enter Sentence: ")
print S.upper() # This will capitalize all
print S.lower() # This will lowercase all
Okay, well not to fully solve the problem for you i would do something like this:
Now that does nothing at all, just shows you an idea, what you need to do is have another variable and add the letter to it each iteration of the loop.. see if you can do that yourself.
If you have any questions about how this works just post!
python Syntax (Toggle Plain Text)
S = raw_input("Enter a sentence:") vowels = 'aeiou' for letter in S: if letter in vowels: #letter is a vowel letter.upper()
If you have any questions about how this works just post!
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Feb 2009
Posts: 5
Reputation:
Solved Threads: 0
Thanks, but I think I still need further clarification. Sorry if I'm being stupid, but this is seriously my first day at attempting to program.
So i understand that I need to add another variable that would be defined as the vowels (like v). But I think I'm still a bit lost after that. For instance i can't just capitalize v?
I know I can't (cuz when I tried it didn't work lol). So that means, that I need to define the vowels within the sentence. (the V in the S) And that's the part I am not understanding now. (And of course, i don't want you to write the program lol, just explain)
So i understand that I need to add another variable that would be defined as the vowels (like v). But I think I'm still a bit lost after that. For instance i can't just capitalize v?
Python Syntax (Toggle Plain Text)
V="aeiou" V.upper()
I know I can't (cuz when I tried it didn't work lol). So that means, that I need to define the vowels within the sentence. (the V in the S) And that's the part I am not understanding now. (And of course, i don't want you to write the program lol, just explain)
program should be like this
1) get sentence from user
2) check every latter if it is a vowel
3) if it is make it upper case.
you probably figured that out already. now to to thinks simpler first write code first reads every latter one by one. for example when user enter a sentence like
enter a sentence: hello
make it print
h
e
l
l
o
if you can do that rest is pretty easy, instead of printing it make it check if it is a or e or i or o.... if it is make it uppercase.
if you can get it run post it here. people can probably show you some more stuff. if you can't get it run. post it and we can tell you what you are doing wrong.
1) get sentence from user
2) check every latter if it is a vowel
3) if it is make it upper case.
you probably figured that out already. now to to thinks simpler first write code first reads every latter one by one. for example when user enter a sentence like
enter a sentence: hello
make it print
h
e
l
l
o
if you can do that rest is pretty easy, instead of printing it make it check if it is a or e or i or o.... if it is make it uppercase.
if you can get it run post it here. people can probably show you some more stuff. if you can't get it run. post it and we can tell you what you are doing wrong.
So at the end of my loop i gave you you need to reconstruct the sentence so find a way to peice it back together. Here is a code snippet which might help:
And what you need to do, is if that for every time through the loop you check to see if the letter is a vowel, if it is then the if statement will be true and run, so then you would have to use that [icode]letter= letter.upper()[/code] code and that would capitalize your letter ready to be added onto the second string.
python Syntax (Toggle Plain Text)
first = "hello" second = "" for letter in first: second += letter print second #prints "Hello"
And what you need to do, is if that for every time through the loop you check to see if the letter is a vowel, if it is then the if statement will be true and run, so then you would have to use that [icode]letter= letter.upper()[/code] code and that would capitalize your letter ready to be added onto the second string.
Last edited by Paul Thompson; Feb 13th, 2009 at 4:12 pm.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
you did it already:
python Syntax (Toggle Plain Text)
word = "Hello" for letter in word: #this iterates through every letter in the variable word #and to check if A in B then a = "1" b = "123" if a in b: #this will be tru, a is in b
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
hi guys i tried out wat u said but its showing error:name error
my code was like this:
first=input("Enter a sentence :\n")
second=""
vowels='aeiou'
for i in first:
if i in vowels:
second=second+i.upper()
else :
second=second+i
print second
i've one more doubt about assigning the value of vowel i.e
what is the difference between vowels='aeiou' and
vowels=['a','e','i','o','u'] and what will be the changes in coding if we initialize it in a list??
my code was like this:
first=input("Enter a sentence :\n")
second=""
vowels='aeiou'
for i in first:
if i in vowels:
second=second+i.upper()
else :
second=second+i
print second
i've one more doubt about assigning the value of vowel i.e
what is the difference between vowels='aeiou' and
vowels=['a','e','i','o','u'] and what will be the changes in coding if we initialize it in a list??
There would be no changes using a list of vowels, the 'in' operator works the same way:
using vowels = 'aeiou' is much simpler.
python Syntax (Toggle Plain Text)
first = raw_input("Enter a sentence :\n") second = "" #vowels = 'aeiou' vowels = ['a','e','i','o','u'] for i in first: if i in vowels: second = second+i.upper() else : second = second+i print second
drink her pretty
![]() |
Other Threads in the Python Forum
- Previous Thread: Urllib suddenly stopped working
- Next Thread: How to choose which version of python is used by IDLE?
| Thread Tools | Search this Thread |
accessdenied apache application argv array beginner book builtin change chmod converter countpasswordentry curved dan08 dictionary dynamic edit enter examples file filename float format function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple smtp software sprite statictext string strings syntax table tennis terminal text textarea thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






