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: {{ and }}.
Method 1, which is what I'd actually do: use a string.Template instead.
>>> from string import Template
>>> Template(r'\textbf{This and that} plus \textbf{$val}').substitute(val='6')
'\\textbf{This and that} plus \\textbf{6}'
Method 2: add extra braces. Could do this using a regexp.
>>> r'\textbf{This and that} plus \textbf{val}'.format(val='6')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
KeyError: 'This and that'
>>> r'\textbf{{This and that}} plus \textbf{{{val}}}'.format(val='6')
'\\textbf{This and that} plus \\textbf{6}'
(possible) Method 3: use a custom string.Formatter. I haven't had cause to do this myself so I don't know enough of the details to be helpful.
Best Answer
You need to double the
{{
and}}
:Here's the relevant part of the Python documentation for format string syntax: