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

Recommended Answers

All 9 Replies

Okay, well not to fully solve the problem for you i would do something like this:

S = raw_input("Enter a sentence:")

vowels = 'aeiou'

for letter in S:
    if letter in vowels:
        #letter is a vowel
        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!

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?

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.

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:

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 letter= letter.upper() code and that would capitalize your letter ready to be added onto the second string.

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") ])

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"

you did it already:

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

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= 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:

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

using vowels = 'aeiou' is much simpler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.