私が実際にやっている方法1:代わりに文字列テンプレートを使用する。
>>> from string import Template
>>> Template(r'\textbf{This and that} plus \textbf{$val}').substitute(val='6')
'\\textbf{This and that} plus \\textbf{6}'
方法2:追加の中括弧を追加します。これは正規表現を使って行うことができます。
>>> r'\textbf{This and that} plus \textbf{val}'.format(val='6')
Traceback (most recent call last):
File "", line 1, in
KeyError: 'This and that'
>>> r'\textbf{{This and that}} plus \textbf{{{val}}}'.format(val='6')
'\\textbf{This and that} plus \\textbf{6}'
(可能な)方法3:カスタムのstring.Formatterを使用します。私は自分自身でこれを行う理由はなかったので、私は助けになるほどの細部を知らない。