开发者

Python string formatting special characters

How do you make the following code work?

example = "%%(test)%" % {'te开发者_开发问答st':'name',}
print example

Where the desired output is "%name%"

Thanks


An alternative is to use the new Advanced String Formatting

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


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜