擅长:python、mysql、java
<p>您需要将<code>a</code>重新分配给<code>a = a.format(one=one, two=two)</code>,或者只返回它。你知道吗</p>
<pre><code>return a.format(one=one, two=two)
</code></pre>
<p><code>a.format</code>不会更改原始字符串<code>a</code>,字符串是不可变的,因此<code>a.format</code>所做的就是创建一个新字符串。任何时候修改一个字符串都会创建一个新对象。除非使用连接,否则要更改<code>a</code>的值,需要将<code>a</code>重新分配给新对象。你知道吗</p>
<p><code>str.replace</code>是人们被抓的另一个例子:</p>
<pre><code>In [4]: a = "foobar"
In [5]: id(a)
Out[5]: 140030900696000
In [6]: id(a.replace("f","")) # new object
Out[6]: 140030901037120
In [7]: a = "foobar"
In [8]: a.replace("f","")
Out[8]: 'oobar'
In [9]: a # a still the same
Out[9]: 'foobar'
In [10]: id(a)
Out[10]: 140030900696000
In [11]: a = a.replace("f","") # reassign a
In [12]: id(a)
Out[12]: 140030900732000
In [13]: a
Out[13]: 'oobar'
</code></pre>