2

The string already has '{' inside it. Now I want to use python format method.

a = "{foo{}}"
b = a.format("bar")

result should be {foobar}

There are many ways to solve the problem but I want to know is there a way to skip first '{'.

Anshuman
  • 421
  • 4
  • 11

2 Answers2

10

To have a regular brace, use it twice in a format string.

>>> "{{foo{}}}".format("bar")
'{foobar}'
Konrad Borowski
  • 11,584
  • 3
  • 57
  • 71
0

You could use the %x syntax as well:

a = "{foo%s}"
b = a % 'bar'

Which will return '{foobar}' just fine.

FYI: to print % in a string you would use %%.

meissner_
  • 556
  • 6
  • 10