Tabs in txt file vs terminal
<p>I'm learning Python at the moment and I'm wondering why tabs look a little different in a txt file than when written to the terminal.</p>

<p>In particular, I've run this script</p>

<pre><code>my_file = open('power.txt', 'w')
print( 'N \t\t2**N\t\t3**N' )
print( '---\t\t----\t\t----' )
my_file.write( 'N \t\t2**N\t\t3**N\n' )
my_file.write( '---\t\t----\t\t----\n' )

for N in range(11) :
    print( "{:d}\t\t{:d}\t\t{:d}".format(N, pow(2,N), pow(3,N)) )
    my_file.write( "{:d}\t\t{:d}\t\t{:d}\n".format(N, pow(2,N), pow(3,N)) )
my_file.close()
</code></pre>

<p>In it you can see that I'm writing the same thing to both the terminal and the power.txt file.  What I see in the terminal is </p>

<p><a href="http://i.stack.imgur.com/3eiPQ.png" rel="nofollow"><img src="http://i.stack.imgur.com/3eiPQ.png" alt="enter image description here"></a></p>

<p>and what I see in the txt file is</p>

<p><a href="http://i.stack.imgur.com/0wxIQ.png" rel="nofollow"><img src="http://i.stack.imgur.com/0wxIQ.png" alt="enter image description here"></a></p>

<p>As you can see the third column lines up much nicer in the terminal than the txt file.  I've got two questions:</p>

<ol>
<li>As I'm writing the exact same data to both, why does the information display differently in the txt file?</li>
<li>If I wanted the columns in the txt file to line up like they do in the terminal (to improve readability), how could I alter my script to do so?</li>
</ol>

<p>Don't rely on tabs, they're application/console dependent. Use <code>str.format</code> instead (<a href="https://docs.python.org/2/library/string.html" rel="nofollow">format specification</a>)</p>

<p>BTW <code>pow(2,N)</code> is a floating number. You want integral powers: <code>2**N</code></p>

<p>Standalone example writing to standard output:</p>

<pre><code>import sys

my_file = sys.stdout
header = "{:&lt;10s}{:&lt;10s}{:&lt;10s}\n".format('N','2**N','3**N' )

my_file.write(header)

for N in range(11) :
    row =  "{:&lt;10d}{:&lt;10d}{:&lt;10d}\n".format(N, 2**N, 3**N)
    my_file.write(row)
</code></pre>

<p>result:</p>

<pre><code>N         2**N      3**N      
0         1         1         
1         2         3         
2         4         9         
3         8         27        
4         16        81        
5         32        243       
6         64        729       
7         128       2187      
8         256       6561      
9         512       19683     
10        1024      59049     
</code></pre>

<p>(you could use legacy C-style format like <code>%20s</code> or <code>%-10s</code> but it's frowned upon now as deprecated, and the <code>format</code> interface is much much more powerful)</p>

python file