Return last character of string in python
<p>I want to print the last character of string in python reading from the file. I am calling as <code>str[-1]</code> But not working. </p>

<p><strong>t.txt contains</strong></p>

<pre><code>Do not laugh please!        9

Are you kidding me?     4
</code></pre>

<p>My code is </p>

<pre><code>with open('t.txt', 'r') as f:
            for line in f:
                print(line )
                print(line[-1])
                #break
</code></pre>

<p>But it is not printing anything. </p>

<p>The last character of every line is a <em>newline character</em>. You can <a href="https://docs.python.org/2/library/string.html#string.strip" rel="nofollow">strip</a> it:</p>

<pre><code>print(line.strip()[-1])  
# or print(line.rstrip()[-1])
</code></pre>

python