another python question!

Started by
7 comments, last by Zahlman 16 years, 10 months ago
okay well if you havent seen my posts before im working out of a book im on the strings chapter question/practice problem/programming. my assignment was to create a program that took in a phrase for example random access memory and write a program that spits out the acronym so RAM. basically just taking the first letter from each word i had no problem figuring this out and programming it. just i have a question on printing it out of a loop. here is my code.

#acronym maker
import string

def main():
	print "Welcome, this program allows you to enter a phrase and then it"
	print "will print out the acronym for that phrase."
	phrase = raw_input("Enter the phrase that you would like to find out the acronym for.")
    
	for each in string.split(phrase):
		firstLetter = each[0]
		print "The acronym for your phrase is,", string.upper(firstLetter) + "."

okay so ive tried it with random things everything prints out it prints out in all caps does everything i want... except when it prints out it comes out like The acronym for your phrase is, R The acronym for your phrase is, A The acronym for your phrase is, M and ive been fooling around and dont know how to get it to not print a new line everytime it goes through the loop. i want all the letters next to each other like RAM. how do i accomplish this? i took print out of the loop to just the regular but that obviously did not work. im not sure how i need to adjust where my print is or what its saying to get it to come out how i would like. so if anyone could help me id appreciate it. thanks heyhohey
Advertisement
Just put commas after the strings you want to print without newlines.

print "foo"print "bar"# prints "foo", then "bar"print "foo",print "bar"# prints "foobar"
Ra
	for each in string.split(phrase):		firstLetter = each[0]		print "The acronym for your phrase is,", string.upper(firstLetter) + "."

What you are doing here is printing the phrase "The Acronym for your ..." and the first letter of every word for each word in your string. What you want to to is build a string out of the first letter of each word and print the string afterwards:
acronym = ""for word in "Random Access memory".split(" "):    acronym += word[0]print acronym.upper()
thank you for that info i tried putting a comma at the end and yes it did not go to a new line but it turned out like this
The acronym for your phrase is,B. The acronym for your phrase is,S. The acronym for your phrase is,P. The acronym for your phrase is,P.
how do i manipulate the loop or print statement so it only says The acronym for your phrase is BSPP or RAM. I dont want it printing a statement for each character of the acronym.

thanks
heyhohey
edit nvm just saw your post crimsonsun
ty for that info you answered what i wanted.
just for reference what does += mean exactly?

thanks
heyhohey
+= means to add the right-side operand to the left-side operand and assign it to the left side-operand. So x += 1 is equivalent to x = x + 1.
Quote:Original post by CrimsonSun
+= means to add the right-side operand to the left-side operand and assign it to the left side-operand. So x += 1 is equivalent to x = x + 1.


OHHHH OKAY THANK YOU VERY MUCH
so acronym += word[0]
is equivalent to firstLetter = firstLetter + each[0]
thank you for the help i changed my code and ran it and it works perfectly tyvm :P i now get why it works that way cause it takes the letter + firstLetter which to start is nothing and adds them together then make them equal firstLetter. the loop goes again with R as the firstLetter and adds the next letter then sets RA together equal as firstLetter then it goes on till it finishes the phrase.

thanks a ton
heyhohey
# No need to import string; in modern versions of Python, these functions are# provided as members of the string objects themselves.print 'The acronym for your phrase is "%s".' % "".join([word[0].upper() for word in phrase.split()])


[smile]
Quote:Original post by Zahlman
*** Source Snippet Removed ***

[smile]


sorry but what you posted just really confused me i know from my book that % is a string formatter thingy but im just confused at looking what your doing. is it bad for me to do things the old way? cause my books like a year old or two maybe but im used to it for now. so basically should i just learn it this way then later make the change or try to do it now?
print 'The acronym for your phrase is "%s".' \ # self-explanatory% \ # And substitute in for the %s, the following:"".join( \ # The result of using empty strings to join up all of these items:[word[0].upper() \ # The uppercase version of the first letter of the word,for word \ # for each item (using the label "word" to refer to those items in turn)in \ # self-explanatory :)phrase.split()]) # the split-up version of the string (split on whitespace)


The "".join() is simply a way of taking a list of strings and concatenating them all, the same way that the += works. We could specify some text to put in between the strings at the join-points, but we don't want any, so we use a blank string. Then, it's just a matter of specifying the strings we want. We want one string for each word, so we make a list comprehension that creates the appropriate string corresponding to each word, using phrase.split() to give us the words.

In Python, everything is an object - including strings - and they often have quite a few functions that can be called *upon* them. So "foo".upper() results in "FOO"; instead of asking the string module to give us the uppercase version of "foo", we ask "foo" to give it the uppercase version of *itself*.

This topic is closed to new replies.

Advertisement