By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,876 Members | 1,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,876 IT Pros & Developers. It's quick & easy.

print and % operator

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Ruchika wrote:
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?


http://www.python.org/doc/2.3.4/lib/...q-strings.html

--
--------------------------------------------------------------------
Aaron Bingham
Application Developer
Cenix BioScience GmbH
--------------------------------------------------------------------

Jul 18 '05 #2

P: n/a
Ruchika <ru***********@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
Jul 18 '05 #3

P: n/a
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-...s.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
Jul 18 '05 #4

P: n/a
Ruchika wrote:
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
Jul 18 '05 #5

P: n/a
Peter Hansen <pe***@engcorp.com> writes:
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.

--
Brian (remove the sport for mail)
http://www.et.dtu.dk/staff/be
Jul 18 '05 #6

P: n/a
Brian Elmegaard wrote:
Peter Hansen <pe***@engcorp.com> writes:

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."


Yes, I'm sure. http://docs.python.org/tut/node9.html

-Peter
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.