Why does '\b' look like invalid as the last character of sentence in Python?
<p>I feel puzzled about '\b'.
I know that '\b' means backspace, but in Python, if the last character of sentence it seems invalid.</p>

<p>Example:</p>

<pre><code>&gt;&gt;print 'abc\be'
&gt;&gt;abe
&gt;&gt;print 'abc\b'
&gt;&gt;abc
</code></pre>

<p>Why?</p>

<p>Andï¼another example on OSX/python2.7.10 <strong>IPython</strong>:</p>

<pre><code>&gt;&gt; import sys
&gt;&gt; sys.stdout.write('abc\b')
&gt;&gt; abc
&gt;&gt; sys.stdout.write('abc\be')
&gt;&gt; abe
</code></pre>

<p>There's an implied newline after the <code>print</code> finishes, which causes a newline immediately after the <code>\b</code> is echoed. This causes the cursor to move to the next line, so there won't be anything overwriting the <code>c</code> from the previous line.</p>

<p>If you did something like:</p>

<pre><code>print 'abc\b', 'def'
</code></pre>

<p>you would see output like:</p>

<pre><code>ab def
</code></pre>

<p>i.e. it's not 'invalid' at the end of a sentence, it's just that because you immediately print a newline, nothing gets an opportunity to overwrite the character that backspaced into.</p>

<p>To make this a little bit more clear (hopefully) - taken by typing the lines into python directly:</p>

<p>print adds the newline, if we use <code>sys.stdout.write</code>, it won't add a newline automatically:</p>

<pre><code>&gt;&gt;&gt; import sys
&gt;&gt;&gt; sys.stdout.write('abc')
abc&gt;&gt;&gt; sys.stdout.write('abc\b')
ab&gt;&gt;&gt;
</code></pre>

python character