开发者

Python string formatting special characters

开发者 https://www.devze.com 2022-12-09 07:15 出处:网络
How do you make the following code work? example = \"%%(test)%\" % {\'te开发者_开发问答st\':\'name\',}

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

精彩评论

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