Why does `print foo.communicate()[0]` differ from `print foo.communicate()`"
<p>Here is the situation:</p>

<p>I've got a command I'm running:</p>

<pre><code>import subprocess
foo = subprocess.Popen('ls /', shell=True, stdout=subprocess.PIPE,\
stderr=subprocess.STDOUT)
</code></pre>

<p>Pretty basic, right? And I've figured out that I can do stuff with the output with <code>.communicate()</code>, like this:</p>

<pre><code>print foo.communicate()
</code></pre>

<p>Which works great and produces the output that the documentation for subprocess.communicate suggests it should, a tuple:</p>

<pre><code>('bin\nboot\ncdrom\n[...stuff redacted for brevity...]tmp\nusr\nvar\nvmlinuz\n', None)
</code></pre>

<p>Notice the <code>\n</code> newlines in there. And I've discovered that just asking for the first element of the tuple produces output with newlines, like this:</p>

<pre><code>&gt;&gt;&gt; print foo.communicate()[0]
bin
boot
cdrom
[...stuff redacted for brevity...]
tmp
usr
var
vmlinuz
</code></pre>

<p>But what I don't understand is WHY printing with just the first element produces the newlines. Don't get me wrong, it is great, and I'm glad I can do it without a loop, but I'd like to understand what is going on.</p>

<p>Thanks in advance,</p>

<p>--jed</p>
