On 10 September 2013 08:58, Thabile Rampa <thabilera...@gmail.com> wrote: > On Aug 27, 2013, at 3:40 AM, isaac Eric wrote > > <snip> > >> print "For a circle of radius %s the area is %s" % (radius,area) > <snip> >> Question: What is the purpose of %s ? > > I will admit that this is homework for me. However, this is more for my log > book and not for marks. > > According to my understanding, the purpose of the %s is to turn the numbers, > which the program has recognized as numbers, into strings, so that they fit > in the print command without any syntax errors. > > Could you guide me in the right direction if it is completely off?
You are correct. '%s' is used to convert numbers (or other non-string things) into strings so that they can be used in places where text is required. In the particular case of the print command, this is done automatically so printing a number directly works just fine: >>> a = 123.0 >>> print a 123.0 >>> print 'The size is:', a The size is: 123.0 The '%s' form though allows you to insert the string representation of the number at the appropriate place in the string making it a bid cleaner to read e.g.: >>> radius = 4 >>> pi = 3.1412654 >>> area = pi * radius ** 2 This one: >>> print 'For a circle of radius', radius, 'the area is', area For a circle of radius 4 the area is 50.2602464 is equivalent to this one: >>> print 'For a circle of radius %s the area is %s' % (radius, area) For a circle of radius 4 the area is 50.2602464 If you just want to get the string representation of a number you can just use the str() function: >>> area 50.2602464 >>> str(area) '50.2602464' Oscar _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor