February 6th, 2005, 05:30 PM
Computing with strings
I had a python question which I can't solve. I am not to good at this stuff, I admit. Here is the question.
Write a program that allows the user to type in a phrase and then outputs the acronym for that phrase. The acronym should be all uppercase, even if the words in the phrase are not capitalized.
Here is what I have so far. I know its totally wrong but I just am not sure what to do. I thought maybe you need to get the length of the phrase with len or something? I am totally lost, heh. I would appreciate any help in this matter.
print "This program takes a phrase and gives its acronym."
phrase = raw_input("Enter in the phrase: ")
print "The acronym is",
February 6th, 2005, 05:45 PM
First I need to ask you if this is a homework assignment, before I can give you a working example . In any case, what you should do is to loop over each word in the string – you can do this by splitting the string on white-spaces and then looping over the returned list.
Inside the loop, you could use the strip() method to remove any punctuation from around the word; this is recommended although not technically required!
Edit: you can find an example of how to do this here:
After that: take the first character of the word (using theWord) and convert it to uppercase before appending it to a list – defined outside of the loop.
When the loop has finished you should have a list of all the first letters in your phrase, which can be joined together to form your acronym
hope this helps,
Last edited by netytan; February 6th, 2005 at 05:50 PM.
February 6th, 2005, 05:59 PM
Thanks for the quick reply moderator Mark, heh. I will give what you said a try. As for the question its an example my instructor gave but I only half remember what he did because I didn't write it down. I am weird I like to do the examples, sometimes it helps me learn how to do the actually problems.
February 6th, 2005, 06:03 PM
Now, I myself can't think of a way to put those capital letters together, like ICMC. You could do:
>>> i = raw_input("Phrase: ")
Phrase: i crashed my car
>>> i = i.split(' ')
['i', 'crashed', 'my', 'car']
>>> l = 
>>> for item in i:
['I', 'C', 'M', 'C']
And you would do this maybe until len(1) equals 8. Guessing that the phrase isn't going to be long. Hope this helps a bit.
if len(l) == 2:
for item in l:
elif len(l) == 3:
for item in l:
February 6th, 2005, 06:05 PM
Your very welcome user Adam . Anyway, if you have any questions dont hesitate to as. Or if you want a working example that you can follow along with I'd be more than happy to provide one.
February 6th, 2005, 06:10 PM
For a newb like me a working example would be nice. Just to give you a little background on my programming experience, this is the first class I have taken in any programming language so I know nearly nothing about alot of this stuff so you can never simplify things too much for me, heh.
February 6th, 2005, 06:25 PM
Heres a copy of my Python shell. If you've never used this before it is basically a way to interact with Python and see the results right away. Anyway here it is, step by step.
This could also be shortened into a list comprehension however for clarity i've left it like this .
>>> from string import punctuation
>>> phrase = 'One working example... working'
>>> acronym = 
>>> for word in phrase.split():
... word = word.strip(punctuation)
... word = word.upper()
['O', 'W', 'E', 'W']
February 6th, 2005, 06:45 PM
Thanks Mark. That example makes sense, now. What changes would have to be made to do the same thing but in the text editor? It seems like I couldn't get the acronym to have periods between letters if I used the text editor. The shell worked just fine.
February 6th, 2005, 06:58 PM
Generally when you convert an example from the shell you need to remove the shell prompts (>>>) and the periods (...), as well as any output sent from the interpreter. You also need to remember to use the [b]print[/i] keyword if you want something to be displayed. So we end up with something like this:
from string import punctuation
phrase = 'One working example... working'
acronym = 
for word in phrase.split():
word = word.strip(punctuation)
word = word.upper()
February 6th, 2005, 07:16 PM