x = \" \\{ Hello \\} {0} \"
print(x.format(42))
gives me : Key Error: Hello\\\\
I want to print the output: {Hello} 42>
When you're just trying to interpolate code strings I'd suggest using jinja2 which is a full-featured template engine for Python, ie:
from jinja2 import Template
foo = Template('''
#include
void main() {
printf("hello universe number {{number}}");
}
''')
for i in range(2):
print(foo.render(number=i))
So you won't be enforced to duplicate curly braces as the whole bunch of other answers suggest