[Tutor] [Re:] I need help with the following question
Alan Gauld
alan.gauld at btinternet.com
Tue Sep 10 12:40:35 CEST 2013
On 10/09/13 08:58, Thabile Rampa wrote:
>> print "For a circle of radius %s the area is %s" % (radius,area)
> <snip>
>> Question: What is the purpose of %s ?
Oscar has answered your basic question but to add to his comments thee
are other reasons for using the %s rather than str() or simply printing
the variables directly. The %s allows us to add extra information to
control the format of the string produced, for example the total field
length and whether it is left or right justified.
eg Try
>>> "%12s" % "Hello"
>>> "%-12s" % "Hello"
>>> "%-12.4s" % "Hello"
You can read about all the string formatting characters and
their 'extras' here:
http://www.python.org/doc//current/library/stdtypes.html
in Section 6.6.2
Note that in Python 3 this style of string formatting is being
deprecated in favour of the new format method of strings (linked
on the same page above under the str.format() method) which
offers even more options.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list