943,706 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2678
  • Python RSS
Feb 13th, 2009
0

Python Code Help - Capitalize only vowels

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
curiouskitten is offline Offline
5 posts
since Feb 2009
Feb 13th, 2009
0

Re: Python Code Help - Capitalize only vowels

Okay, well not to fully solve the problem for you i would do something like this:
python Syntax (Toggle Plain Text)
  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!
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Feb 13th, 2009
0

Re: Python Code Help - Capitalize only vowels

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?

Python Syntax (Toggle Plain Text)
  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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
curiouskitten is offline Offline
5 posts
since Feb 2009
Feb 13th, 2009
0

Re: Python Code Help - Capitalize only vowels

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.
Reputation Points: 31
Solved Threads: 5
Light Poster
yilmazhuseyin is offline Offline
48 posts
since Oct 2006
Feb 13th, 2009
0

Re: Python Code Help - Capitalize only vowels

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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Feb 13th, 2009
0

Re: Python Code Help - Capitalize only vowels

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") ])
Reputation Points: 94
Solved Threads: 48
Posting Whiz
BearofNH is offline Offline
321 posts
since May 2007
Feb 13th, 2009
0

Re: Python Code Help - Capitalize only vowels

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"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
curiouskitten is offline Offline
5 posts
since Feb 2009
Feb 13th, 2009
0

Re: Python Code Help - Capitalize only vowels

you did it already:
python Syntax (Toggle Plain Text)
  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
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Feb 14th, 2009
0

Re: Python Code Help - Capitalize only vowels

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??
Reputation Points: 16
Solved Threads: 0
Light Poster
karthik.c is offline Offline
48 posts
since Feb 2009
Feb 14th, 2009
0

Re: Python Code Help - Capitalize only vowels

There would be no changes using a list of vowels, the 'in' operator works the same way:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Urllib suddenly stopped working
Next Thread in Python Forum Timeline: How to choose which version of python is used by IDLE?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC