print and % operator

Alex Martelli aleaxit at yahoo.com
Thu Sep 9 03:28:33 EDT 2004


Ruchika <ruchika_khera at hotmail.com> wrote:

> Hi,
> 
> I am new to Python, so this may be a very simple question for most of
> you. What does the % operator stand for in Python? I came across a

Between numbers, it means "remainder of division", aka "modulo"; if the
left-hand operator is a string, it means "formatting".

> script that uses the % operator as follows -
> 
> def sync(delf,name):
>     os.popen3( 'P4 -s sync -f %s' % name)

Here it's formatting, as the LHO is a string,

> 
> I would suspect that this just replaces the %s with the value of name.
> Is % before name required? Should there be a space between % and name?

The % is indeed required, spaces are optional (like most always between
operators and other tokens -- no difference there between the operator %
and other operators such as + * and so on).

 
> On a similar note, how can I print the value of name using the print
> statement? Should print %name print the value of name?

No, there is no unary version of % -- just use:

    print name

> 
> Most of the time, when I add some print statements to my script I get
> Autoindent error. Can someone tell me what this error is and how to
> fix it?

Sounds like a problem with your editor, not with Python.


Alex



More information about the Python-list mailing list