next up previous [pdf]

Next: Dictionaries Up: Integrating Python with SCons Previous: Variables

String substitution

While using variables is convenient, formatting them in the fashion shown above is not convenient. An easier way to format variables for strings is to use string substitution. String substitution works in the same way as the C - printf function works, i.e. we place markers that indicate where values should be substituted and what format they should be in. The above example using string substitution is:


\begin{verbatimtab}[4]
nx = 100
nz = 100
\par
Flow('model',None,
'''
sfspike n1=%d n2=%d
''' % (nx,nz) )
\end{verbatimtab}

In this statement, both nx, and nz are formatted as integers, and contained inside a tuple. All variables to be used for string substitution must be contained within the tuple at the end of the string statement. For reference, all C-printf like formatting choices are available in Python as well. Note: treat booleans as integers in Python.




2011-11-03