2

How do you make the following code work?

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

Where the desired output is "%name%"

Thanks

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
user197196
  • 23
  • 1
  • 1
  • 4

2 Answers2

7

An alternative is to use the new Advanced String Formatting

>>> example = "%{test}%".format(test="name")
>>> print example
%name%
Peter Hoffmann
  • 56,376
  • 15
  • 76
  • 59
5
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
  • 64,979
  • 15
  • 154
  • 145
  • Ah I knew it would be simple! I tried the classic backslash to cancel special characters then got stuck. Thanks! – user197196 Oct 27 '09 at 10:02
  • 2
    You omitted the helpful reference information: http://docs.python.org/library/stdtypes.html#string-formatting-operations. – S.Lott Oct 27 '09 at 10:23