Python Code Help - Capitalize only vowels

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 5
Reputation: curiouskitten is an unknown quantity at this point 
Solved Threads: 0
curiouskitten curiouskitten is offline Offline
Newbie Poster

Python Code Help - Capitalize only vowels

 
0
  #1
Feb 13th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 909
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Python Code Help - Capitalize only vowels

 
0
  #2
Feb 13th, 2009
Okay, well not to fully solve the problem for you i would do something like this:
  1. S = raw_input("Enter a sentence:")
  2.  
  3. vowels = 'aeiou'
  4.  
  5. for letter in S:
  6. if letter in vowels:
  7. #letter is a vowel
  8. letter.upper()
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!
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: curiouskitten is an unknown quantity at this point 
Solved Threads: 0
curiouskitten curiouskitten is offline Offline
Newbie Poster

Re: Python Code Help - Capitalize only vowels

 
0
  #3
Feb 13th, 2009
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?

  1. V="aeiou"
  2. 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)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 48
Reputation: yilmazhuseyin is an unknown quantity at this point 
Solved Threads: 5
yilmazhuseyin's Avatar
yilmazhuseyin yilmazhuseyin is offline Offline
Light Poster

Re: Python Code Help - Capitalize only vowels

 
0
  #4
Feb 13th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 909
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Python Code Help - Capitalize only vowels

 
0
  #5
Feb 13th, 2009
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:
  1. first = "hello"
  2. second = ""
  3. for letter in first:
  4. second += letter
  5.  
  6. print second
  7. #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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: Python Code Help - Capitalize only vowels

 
0
  #6
Feb 13th, 2009
Possibly not the easiest to explain, but a completely different approach would be "".join([ [letr.lower(),letr.upper()][letr in 'aeiou'] for letr in list("whatever was input") ])
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: curiouskitten is an unknown quantity at this point 
Solved Threads: 0
curiouskitten curiouskitten is offline Offline
Newbie Poster

Re: Python Code Help - Capitalize only vowels

 
0
  #7
Feb 13th, 2009
I guess wha'm not understanding is that I do'nt know how to write the code for "a letter in word" or "A in B"
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 909
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Python Code Help - Capitalize only vowels

 
0
  #8
Feb 13th, 2009
you did it already:
  1. word = "Hello"
  2. for letter in word:
  3. #this iterates through every letter in the variable word
  4. #and to check if A in B then
  5. a = "1"
  6. b = "123"
  7. if a in b:
  8. #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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 46
Reputation: karthik.c is an unknown quantity at this point 
Solved Threads: 0
karthik.c's Avatar
karthik.c karthik.c is offline Offline
Light Poster

Re: Python Code Help - Capitalize only vowels

 
0
  #9
Feb 14th, 2009
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??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Python Code Help - Capitalize only vowels

 
0
  #10
Feb 14th, 2009
There would be no changes using a list of vowels, the 'in' operator works the same way:
  1. first = raw_input("Enter a sentence :\n")
  2. second = ""
  3. #vowels = 'aeiou'
  4. vowels = ['a','e','i','o','u']
  5. for i in first:
  6. if i in vowels:
  7. second = second+i.upper()
  8. else :
  9. second = second+i
  10.  
  11. print second
using vowels = 'aeiou' is much simpler.
drink her pretty
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC