Python - Removing \n when using default split()?
<p>I'm working on strings where I'm taking input from the command line. For example, with this input:</p>

<pre><code>format driveName "datahere"
</code></pre>

<p>when I go string.split(), it comes out as:</p>

<pre><code>&gt;&gt;&gt; input.split()
['format, 'driveName', '"datahere"']
</code></pre>

<p>which is what I want.</p>

<p>However, when I specify it to be string.split(" ", 2), I get:</p>

<pre><code>&gt;&gt;&gt; input.split(' ', 2)
['format\n, 'driveName\n', '"datahere"']
</code></pre>

<p>Does anyone know why and how I can resolve this? I thought it could be because I'm creating it on Windows and running on Unix, but the same problem occurs when I use nano in unix.</p>

<p>The third argument (data) could contain newlines, so I'm cautious not to use a sweeping newline remover. </p>

<p>Use <code>None</code> to get the default whitespace splitting behaviour with a limit:</p>

<pre><code>input.split(None, 2)
</code></pre>

<p>This leaves the whitespace at the <em>end</em> of <code>input()</code> untouched.</p>

<p>Or you could strip the values afterwards; this removes whitespace from the start and end, not the middle, of each resulting string, just like <code>input.split()</code> would:</p>

<pre><code>[v.strip() for v in input.split(' ', 2)]
</code></pre>

python