2

How do you make the following code work?

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

Where the desired output is "%name%"

Thanks

0

2 Answers 2

7

An alternative is to use the new Advanced String Formatting

>>> example = "%{test}%".format(test="name")
>>> print example
%name%
5
example = "%%%(test)s%%" % {'test':'name',}
print example

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

2
  • Ah I knew it would be simple! I tried the classic backslash to cancel special characters then got stuck. Thanks!
    – user197196
    Oct 27, 2009 at 10:02
  • 2
    You omitted the helpful reference information: docs.python.org/library/….
    – S.Lott
    Oct 27, 2009 at 10:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.