简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM