Python string special formatting characters

How can you do the following code?

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

If the desired result is: "% name%"

thanks

+3
source share
2 answers

An alternative is to use the new Advanced String Formatting

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

%(key)sis a placeholder for the string identified key. %%performs %when using an operator %.

+5
source

Source: https://habr.com/ru/post/1721166/


All Articles