Python: id() behavior in the Interpreter
<p>I came across this weird behaviour which happens only in an interactive Python session, but not when I write a script and execute it. </p>

<p>String is an immutable data type in Python, hence:</p>

<pre><code>&gt;&gt;&gt; s2='string'
&gt;&gt;&gt; s1='string'
&gt;&gt;&gt; s1 is s2
True
</code></pre>

<p>Now, the weird part:</p>

<pre><code>&gt;&gt;&gt; s1='a string'
&gt;&gt;&gt; s2='a string'
&gt;&gt;&gt; s1 is s2
False
</code></pre>

<p>I have seen that having a whitespace in the string causes this behaviour. If I put this in a script and run it, the result is True in both cases.</p>

<p>Would anyone have a clue about this? Thanks.</p>

<p>EDIT:</p>

<p>Okay, the above question and answers give some ideas. Now here is another experiment:</p>

<pre><code>&gt;&gt;&gt; s2='astringbstring'
&gt;&gt;&gt; s1='astringbstring'
&gt;&gt;&gt; s1 is s2
True
</code></pre>

<p>In this case the strings are definitely longer than <code>'a string'</code>, but are still having the same identifiers.</p>
