1

how can you format a string of this form in Python 3?

'''{name}{{name}}'''.format(name="bob")

the desired output is: bob{bob}, but the above gives: bob{name}.

one solution is to add another argument to format:

'''{name1}{name2}'''.format(name1="bob", name2="{bob}")

but this is excessive. is there a way to properly escape { such that inner {x} can still be interpolated and one can only pass a single name to format?

  • 1
    Err, did you try `'''{name}{{{name}}}'''.format(name="bob")`? – cs95 Jun 05 '19 at 13:58
  • note: this would be the same with single-quote strings as well, the whole point of `{{` / `}}` is to escape `{` / `}` so it would be ignored by `.format()` and be taken as a literal char – Adam.Er8 Jun 05 '19 at 13:58

1 Answers1

5

Add one more level of {}:

'''{name}{{{name}}}'''.format(name="bob")

which outputs:

bob{bob}
Austin
  • 25,759
  • 4
  • 25
  • 48