How can I print literal curly-brace characters in python string and also use .format on it

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

x = " { Hello } {0} "
print x.format(42)

gives me : Key Error: Hello\

I want to print the output: {Hello} 42

Answers

You need to double the Modèle:And:

>>> x = " {{ Hello }} {0} "
>>> print x.format(42)
  { Hello } 42  

http://docs.python.org/library/string.html#formatstrings http://docs.python.org/library/string.html#formatstrings

Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: Modèle:And.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/5466451/how-can-i-print-literal-curly-brace-characters-in-python-string-and-also-use-fo

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils