[Tutor] (no subject)

Alan Gauld alan.gauld at btinternet.com
Sat Dec 19 19:21:20 CET 2009


"Richard Hultgren" <hultgren1946 at yahoo.com> wrote

> I am a newcomer, I guess I'm have trouble thinking like a computer yet.
> My question is: in this program:
>>> resp = raw_input("What's your name? ")
>
>>> print "Hi, %s, nice to meet you" % resp
>
> does %s mean 'place the user response here' and secondly what and why
> do I put % resp at the end of the print statement?

This is known as a format string and the structure is:

<format_string> % <arguments>

So the format string in your case is

"Hi, %s, nice to meet you"

And every % marker in the string expects a correspomnding argument

The % separates the string from its arguments

and in your case tyou only have %s so you only need one argument which is

resp.

So the format operator(%) substitures the argument resp into the format
string in place of the %s to give the required result.

The format string can contain many % markers each indicating things like
different types, how much space to use, wjether to right pr left justify 
the value etc.

You can find out more in vthe Simple sequences topic of my tutor
or by searching for format strings in the docs.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list