Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 14 Online
OpenStudy (dmw_ocw):

I have the following code: phrase = raw_input ('Enter a phrase to get the Acronym for it: ') phrase_up_split = phrase.upper().split() Say the user enters American Medical Association. How can I print out the first letter of each word in the list?

OpenStudy (espex):

for word in phrase_up_split: print (word[0])

OpenStudy (dmw_ocw):

Damn I hate that I spent over an hour on this and it was so simple. Oh well. Thanks alot.

OpenStudy (e.mccormick):

If you are using raw_input, that is 2.x, so it is not print (word[0]).... and you might want it to all be on one line. And you might want to strip the spacing. >>> phrase = "American Medical Association" >>> phrase_up_split = phrase.upper().split() >>> for word in phrase_up_split: print word[0] A M A >>> for word in phrase_up_split: print word[0], A M A >>> temp='' >>> for word in phrase_up_split: temp = temp + word[0] >>> print temp AMA

OpenStudy (anonymous):

print('blah') works in 2,7,5

OpenStudy (e.mccormick):

That's true. 2.7.x is transitional. The print function is available as well as the older way of printing. However, it also changes how to keep it on one line.

OpenStudy (e.mccormick):

In 3.3.2: >>> phrase = "American Medical Association" >>> phrase_up_split = phrase.upper().split() >>> for word in phrase_up_split: print(word[0]) ... A M A >>> for word in phrase_up_split: print(word[0],end='') ... AMA >>> In 2.7.2: >>> for word in phrase_up_split: print(word[0],end='') SyntaxError: invalid syntax >>> Seems the print function is not fully implemented in 2.7.2....

OpenStudy (e.mccormick):

As a side note, this is a great example of why they are changing print in 3.x. What I had to do to get AMA on one line and without extra spaces in 2.7.2 is a bit messy. It uses an extra variable for a bit. However, in 3.x, all that goes away.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!