views:

371

answers:

2

How do you make the following code work?

example = "%%(test)%" % {'test':'name',}
print example

Where the desired output is "%name%"

Thanks

+3  A: 
example = "%%%(test)s%%" % {'test':'name',}
print example

%(key)s is a placeholder for a string identified by key. %% escapes % when using the % operator.

Ferdinand Beyer
Ah I knew it would be simple! I tried the classic backslash to cancel special characters then got stuck. Thanks!
You omitted the helpful reference information: http://docs.python.org/library/stdtypes.html#string-formatting-operations.
S.Lott
+5  A: 

An alternative is to use the new Advanced String Formatting

>>> example = "%{test}%".format(test="name")
>>> print example
%name%
Peter Hoffmann