Domanda

Come si fa a far funzionare il seguente codice?

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

Dove l'output desiderato è "% name% "

Grazie

È stato utile?

Soluzione

Un'alternativa è utilizzare il nuovo Formattazione avanzata delle stringhe

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

Altri suggerimenti

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

% (key) s è un segnaposto per una stringa identificata da key . %% scappa da % quando si utilizza l'operatore % .

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top