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>Strings are immutable and can't be changed, but what you're trying to do doesn't work with mutable objects either, so mutability (or lack of it) is something of a red herring here.</p>

<p>The real reason is that Python does not work like Excel. Objects do not remember all the operations that have been performed on them, and then re-do those operations when you change any of the information that went into them. For Python to work that way, the language would need to retain either every state that every object had ever been in, or all the operations that had ever been performed on them along with their parameters. Either would make your programs use a lot more memory and run much more slowly. On top of that, suppose you used <code>string1</code> in another expression: that value, too, would need to be updated. In Python 3, <code>print()</code> is a function; should it be called again when the variable printed is changed?</p>

<p>There are languages that work like that, to some extent, but Python isn't one of them. Unless you explicitly arrange things otherwise (by writing and calling a function), Python evaluates an expression once and uses that exact result going forward.</p>

<p>In fact, what you want isn't even possible in Python. When you do string interpolation (the <code>%</code> operation), the code that does it sees only the value of what you're interpolating. It doesn't know that the <code>"6"</code> is coming from a variable called <code>placehold</code>. Therefore it couldn't change the string later even if it wanted to, because there's no way for it to know that there's any relationship between <code>placehold</code> and <code>string1</code>. (Also, consider that you needn't be interpolating a single variable: it could be an expression such as <code>placehold + "0"</code>. So Python would need to remember the entire expression, not just the variable name, to re-evaluate it later.)</p>

<p>You can write your own string subclass to provide the specific behavior you want:</p>

<pre><code>class UpdatingString(str):
    def __str__(self):
        return self % placehold

placehold = "6"
string1 = UpdatingString("this is string one and %s")
print(string1)
placehold = "7"
print(string1)
</code></pre>

<p>But this is fraught with scoping issues (basically, the class needs to be able to see <code>placehold</code>, which means that variable and the class need to be defined in the same function or in the global scope). Also, if you use this special string in an operation with another string, say concatenation, it will almost certainly cease being special. To fix these issues is possible, but... hairy. I don't recommend it.</p>

python string