If strings are immutable, then how is this possible?
<p>I am new to python and was going through the python3 docs. In python strings are said to be immutable, then how is this possible:</p>

<pre><code>if __name__ == '__main__':
        l = 'string'
        print(l)
        l = l[:2]
        print(l)
</code></pre>

<p>returns this output:</p>

<pre><code>string
st
</code></pre>

<p>Informally, <code>l</code> now points to a new immutable string, which is a copy of a part of the old one.</p>

<p>What you cannot do is modify a string in place. </p>

<pre><code>a = "hello"
a[0] = "b"  # not allowed to modify the string `a` is referencing; raises TypeError

print(a)  # not reached, since we got an exception at the previous line
</code></pre>

<p>but you can do this:</p>

<pre><code>a = "hello"
a = "b" + a[1:]  # ok, since we're making a new copy

print(a)  # prints "bello"
</code></pre>

python python-3.x immutability