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?
for word in phrase_up_split: print (word[0])
Damn I hate that I spent over an hour on this and it was so simple. Oh well. Thanks alot.
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
print('blah') works in 2,7,5
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.
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....
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.
Join our real-time social learning platform and learn together with your friends!