# String Formatting when using PythonTex

First of all, many thanks to Geoffrey Poore, the creator of PythonTex. PythonTeX is awesome. I am trying to use it for the first time and really like what it can do. I am having some trouble with using string formatting when using PythonTeX to create LaTeX code. See below for two code snippets. Things work well with Snippet 1, but do not work with Snippet 2. Why is the second snippet failing? How can I use string formatting with PythonTeX.

Thank you for your help.

Snippet 1 (Works)

fig_string = r'''
\begin{figure}[!ht]
\hspace{-0.35in}
\includegraphics{../../mycode/plots_dir/myplot_'''+fig_id+'''.pdf}
\end{figure}
'''

print(fig_string)


Snippet 2 (Throws an error)

    fig_string = r'''
\begin{figure}[!ht]
\hspace{-0.35in}
\includegraphics{../../code20121201/simgridplots_paper/gridplot_{0}.pdf}
\end{figure}
'''.format(fig_id)

print(fig_string)


Error thrown by Snippet 2

This is PythonTeX v0.11

----  Errors and Warnings for py:default:default  ----
Traceback (most recent call last):
* PythonTeX code error on line 149:
File "pythontex-files-figures_doc/py_default_default.py", line 148, in <module>
'''.format(fig_id)
KeyError: 'figure'


Python's .format() method replaces fields delimited by curly braces {}. Unfortunately, LaTeX uses curly braces for other purposes. So you need to escape the LaTeX curly braces. Escaping is accomplished by doubling braces. See http://docs.python.org/2/library/string.html#formatstrings for more details.
So in your case, you need something like \begin{{figure}}[!ht]....