Substituting variables into strings
<p>Currently I know that we can do something like</p>

<pre><code>"This is a string with var %(sub_var)s that will be substituted" % ({'sub_var': 'a1234'})
</code></pre>

<p>That will sub in the <code>sub_var</code> variable into the string. However is there a way to define a string like this:</p>

<pre><code>a = "This is a string with var %(sub_var)s that will be substituted"
</code></pre>

<p>Without defining that <code>sub_var</code> until the string <code>a</code> is actually used? I have some variables that can change depending on the condition, and I don't want to have to keep retyping <code>a</code>.</p>

<p>Thank you!</p>

<p>Store a function in the variable that can be used to make the strings:</p>

<pre><code>&gt;&gt;a='Variable is {}'.format
&gt;&gt;print(a('one'))
Variable is one
&gt;&gt;print(a('two'))
Variable is two
</code></pre>

python string python-2.7