print and % operator

R

Ruchika

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
script that uses the % operator as follows -

def sync(delf,name):
os.popen3( 'P4 -s sync -f %s' % name)

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?

On a similar note, how can I print the value of name using the print
statement? Should print %name print the value of 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?

Thanks for your help.

Ruchika
 
A

Alex Martelli

Ruchika said:
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
 
J

Josef Meile

Hi Ruchika,
> 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?
It is an operator for formating strings. Ie: lets say you want to print
a string followed by an integer value embebbed on a string, then you
could do:

myStr='Test'
value=10
print "String: %s value: %i" % (myStr, value)

This will print:
String: Test value: 10

You could do the same by concatenating the strings:
print "String: " + myStr + "value: " + str(value)

However, the first piece of code is easier to understand and faster.

This link may help:
http://docs.python.org/lib/typesseq-strings.html#l2h-206

>I came across a
script that uses the % operator as follows -

def sync(delf,name):
os.popen3( 'P4 -s sync -f %s' % name)

I would suspect that this just replaces the %s with the value of name.
That's right.
Is % before name required?
Yes, % is required since you included "%s" on the string, so, you have
to match it with some variable/constant. If you use "%i" instead, then
you will have to use an int variable/constant.
>Should there be a space between % and name?
No, you could use:
os.popen3( 'P4 -s sync -f %s'%name)

but it looks ugly and it's difficult to read.
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, if you want to print a variable directly you should use:
print name

if you want to print it inside a string then you should do:
print "This is the value of name: %s" % 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?
Indentation may be wrong, check if you are using the same number of
tabs/spaces on each indentation level.

Regards,
Josef
 
P

Peter Hansen

Ruchika said:
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
script that uses the % operator as follows -

def sync(delf,name):
os.popen3( 'P4 -s sync -f %s' % name)

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?

You _really_ need to go through the tutorial... it answers
this question and others you have had and many others you
will have.

Also consider reading the main FAQ entries soon!

-Peter
 
B

Brian Elmegaard

Peter Hansen said:
You _really_ need to go through the tutorial... it answers
this question

Are you sure? I never found the answer to this. What I found was: "Most
formats work exactly as in C and require that you pass the proper
type; however, if you don't you get an exception, not a core dump."

So you need to know C to use python's tutorial :-(

Josef's link is great though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top