user197196
user197196

Reputation: 23

Python string formatting special characters

How do you make the following code work?

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

Where the desired output is "%name%"

Thanks

Upvotes: 2

Views: 12537

Answers (2)

Peter Hoffmann
Peter Hoffmann

Reputation: 58664

An alternative is to use the new Advanced String Formatting

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

Upvotes: 7

Ferdinand Beyer
Ferdinand Beyer

Reputation: 67147

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

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

Upvotes: 5

Related Questions