[Tutor] I need help with the following question

Dino Bektešević ljetibo at gmail.com
Tue Sep 10 17:37:28 CEST 2013


> Message: 3
> Date: Tue, 10 Sep 2013 09:58:31 +0200
> From: Thabile Rampa <thabilerampa at gmail.com>
> To: tutor at python.org
> Subject: [Tutor] [Re:] I need help with the following question
> Message-ID:
>         <CAOioSuxUREH2o2wbnVyhchcYC7iz64iBWQaVygZBuOso4MXyaA at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> 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?
>
> *Tab Tab*


I'm not a python "brainiac" so I apologize in advance if my answer is
lacking I'll try to be as thorough as possible.
In the light of another recent question on here "Where do I start
python" I want to point out there's been tons of linkage to places
where it's rather easy to find the answer what %s %d and %r are....
One of my favs is the http://learnpythonthehardway.org/book/ and if
you start from exercise 5 onwards you should get a better idea of all
the print options there are in Python and how to use them efficiently.

If you were inquiring SPECIFICALLY about 'formaters' (the %s,d,i,r....
look no further then basic Python manual here:
http://docs.python.org/2/library/stdtypes.html#string-formatting

Lucky for you the %s automatically converts any argument to string
with str() which works for everything in Python, except you might not
like the look of the output.
Be careful to use %i or %d for integers otherwise floats will be rounded up.
Printing strings when using %i will report an error.
I don't think there's any difference between %d (d does NOT stand for
double) and %i.
If you want pretty decimals (1.1) and not floats (1.100000001) use the
decimal module.

Else if you were interested in all the ways you can print in python
just look at the learn python link but here's the crash course anyhow.
I don't think you should have any problems if you ever worked in any
of the big c's before.
Printing in Python 3 onwards needs parentheses around the arguments
you're printing, I think for Python <3 following should work:
Basically if in python you want to print string, python can
automatically connect them in a sentence:
    >>>> print "This"+"is"+"ok"
    This is ok
but that won't work if the print arguments are not strings i.e.:
    >>>> print "This"+"is"+"not" + 6 + "ok"
    TypeError: cannot concatenate string and integer objects
and it's also silly to do that, could you imagine explicitly
converting everything to string?
    >>>> print str(hours)+":"+str(minutes)+":"+str(seconds))
fugly!

What you want to do, resembles the c and c++ printf syntax
    >>>>print "You can add string %s and number %d like this" %(string, number)
    You can add string Banana and number 5 like this

Or also fine, valid only for python>=2.6, and also the way that I
prefer for longer strings is the C# syntax (I think):
    >>>>print "Something is here: {0}".format(anything)
    Something is here: (any number or string)
Because it avoids the necessity for declaring exactly what is it
you're printing and program won't crash, or at least it avoids the
need to add special try catch blocks just for printing. If you see
your output is not what you want you can return and try to work out
exactly what happened.
Hope no serious rules were broken by answering to this question...

Regards,
Dino


More information about the Tutor mailing list