On 10/9/2013 03:58, Thabile Rampa wrote:
> > <div dir="ltr"><div><pre>On Aug 27, 2013, at 3:40 AM, isaac Eric > wrote<br><br><snip> > > <span class="">> print "For a circle of radius %s the area is > %s" % (radius,area)</span> > <snip> > <span class="">> Question: What is the purpose of %s ?</span></pre>I will > admit that this is homework for me. However, this is more for my log book and > not for marks.<br><br></div><div>According to my understanding, the purpose > of the %s is to turn the numbers,<br> > which the program has recognized as numbers, into strings, so that they fit > in the print command without any syntax errors.<br><br></div><div>Could you > guide me in the right direction if it is completely off?<br clear="all"> > </div><div><br></div><font size="4"><b>Tab Tab</b></font></div> > Please post using text email, not html email. In this particular message, your text was pretty easy to extract, but in many cases the html email will become quite garbled by the time the newsreaders pick it up. Besides, it wastes space. The statement, print "For a circle of radius %s the area is %s" % (radius,area) has several areas of interest. Let's decompose it. print - the print statement, which will take whatever expressions it is handed, and convert each to a string before sending them to stdout. In this particular case, there is exactly one expression, and it already is a string. % - the formatting operator, which takes a string on the left side, and a tuple (or other interable) on the right, and combines them together to form a single string. http://docs.python.org/2/library/stdtypes.html#string-formatting-operations "For a circle of radius %s the area is %s" This string contains two %s place-holders. These are not python language syntax, but are clues to the formatting operator that you want a substitution to happen there. So there are two of them, to line up with the two items in the tuple. In this case they are both looking for strings. But %d could have been used to indicate that we want int numbers. And many other combinations could be used, depending on the type of object being used. %08d would mean take the int and left pad it with zeroes till it's 8 characters wide. If you are writing new code, the docs prefer you to use the format method of strings. in this case, the print statement might look something like: print "For a circle of radius {0} the area is {1}".format(radius, area) http://docs.python.org/2/library/stdtypes.html#str.format http://docs.python.org/2/library/string.html#formatstrings -- DaveA _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor