Why don't string variables update when they are changed
<p>Say we have some code like so :</p>

<pre><code>placehold = "6"
string1 = "this is string one and %s" % placehold

print string1

placehold = "7"
print string1
</code></pre>

<p>When run, both of the print statements return as if placehold were ALWAYS 6. However, just before the second statement ran, placehold was changed to 7, so why does it not dynamically reflect in the string?</p>

<p>Also, would you be able to suggest a way to make the string return with 7?</p>

<p>Thank you</p>

<p>When you do:</p>

<pre><code>string1 = "this is string one and %s" % placehold
</code></pre>

<p>You are creating a string <code>string1</code> with <code>%s</code> replaced by the value of <code>placehold</code> On later changing the value of <code>placehold</code> it won't have any impact on <code>string1</code> as string does not hold the dynamic property of variable. In order to reflect the string with changed value, you have to again re-assign the string.</p>

<p>Alternatively, you may use <code>.format()</code> as:</p>

<pre><code>string1 = "this is string one and {}"
placeholder = 6
print string1.format(placeholder)
# prints: this is string one and 6

placeholder = 7
print string1.format(placeholder)
# prints: this is string one and 7
</code></pre>

python string