Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79897

Re: What does %%(%s)s mean/expand to in Python? What does rowshtml += (rowhtml % ((fieldname, ) * 3)) expand to? Kindly explain.

From Peter Otten <__peter__@web.de>
Subject Re: What does %%(%s)s mean/expand to in Python? What does rowshtml += (rowhtml % ((fieldname, ) * 3)) expand to? Kindly explain.
Date 2014-10-29 08:42 +0100
Organization None
References <ee27d38d-8732-485c-a64b-0da2eee1d93b@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.15289.1414568549.18130.python-list@python.org> (permalink)

Show all headers | View raw


satishmlmlml@gmail.com wrote:


> What does %%(%s)s mean in Python?

According to 

https://docs.python.org/2/library/stdtypes.html#string-formatting

in an expression like

"%%(%s)s" % ("foo",)

'%%' expands to '%' and

'%s' expands to 'foo'

so the whole thing gives

>>> "%%(%s)s" % ("foo",)
'%(foo)s'

To this you can then apply another formatting operation that takes a dict as 
its right operand to look up 'foo':

>>> "%(foo)s" % {"foo": "bar"}
'bar'

What you have with "%%(%s)s" is then a template for a template.

> also
> what does
> rowshtml += (rowhtml % ((fieldname,) * 3)) expand to?

(fieldname,)

is a 1-tuple, so

(fieldname,) *3  gives a 3-tuple (fieldname, fieldname, fieldname):

>>> fieldname = "foo"
>>> (fieldname,) * 3
('foo', 'foo', 'foo')

rowhhtml % (fieldname, fieldname, fieldname)

is again string interpolation. Assuming rowhtml contains

"<%s>%s<%s>" may you get

>>> fieldname = "foo"
>>> rowhtml = "<%s>%s</%s>"
>>> rowhtml % ((fieldname,)*3)
'<foo>foo</foo>'

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

What does %%(%s)s mean/expand to in Python? What does rowshtml += (rowhtml % ((fieldname, ) * 3)) expand to? Kindly explain. satishmlmlml@gmail.com - 2014-10-28 23:15 -0700
  Re: What does %%(%s)s mean/expand to in Python? What does rowshtml += (rowhtml % ((fieldname, ) * 3)) expand to? Kindly explain. Peter Otten <__peter__@web.de> - 2014-10-29 08:42 +0100
  Re: What does %%(%s)s mean/expand to in Python? What does rowshtml += (rowhtml % ((fieldname, ) * 3)) expand to? Kindly explain. Christian Gollwitzer <auriocus@gmx.de> - 2014-10-29 09:13 +0100

csiph-web