String Python formattazione di caratteri speciali
-
06-07-2019 - |
Domanda
Come si fa a far funzionare il seguente codice?
example = "%%(test)%" % {'test':'name',}
print example
Dove l'output desiderato è "% name% "
Grazie
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