Removing first character in a String leaves whitespace
<p>I have a textfile that looks like this:</p>

<pre><code>6 Hello World
</code></pre>

<p>I'm reading it in as such:</p>

<pre><code>with open("test.txt") as fp:
    for line in fp:
        l = line.split(" ")
        print(l)
</code></pre>

<p>Which prints me the entire string:</p>

<pre><code>6 Hello World
</code></pre>

<p>I want to remove the '6' character, and just store the string 'Hello World' in another string:</p>

<p>I've done this</p>

<pre><code>newStr = l.replace(l[0],"")
</code></pre>

<p>Which now gives me:</p>

<pre><code> Hello World
</code></pre>

<p>Now I want to remove the leading space, and store the string.
I tried this:</p>

<pre><code>newStr2 = newStr.replace(newStr[0],"")
print(newStr2)
</code></pre>

<p>But this prints:</p>

<pre><code>HelloWorld
</code></pre>

<p>Instead of <code>Hello World</code> (no leading space character)
What am I doing wrong here?
Also, using len() on newStr2 tells me the length is 11, even though it's 10. Is len() including a newline character?</p>

<p>That's because <code>newStr.replace(newStr[0],"")</code> is the same as <code>newStr.replace(" ","")</code> which replaces every instance of a single space with an empty string.  </p>

<p>What you want is <code>lstrip</code> which removes all leading whitespaces  from your string.</p>

<p>More so, to be less verbose, you can simply <em>slice off</em> the leading character from the string and then <code>lstrip</code>:</p>

<pre><code>newStr = l[1:].lstrip()
</code></pre>

<p>On another note, a new line character is a character of length one and that is properly accounted for in <code>len</code>:</p>

<pre><code>&gt;&gt;&gt; len('\n')
1
</code></pre>

<hr>

<p><em>Pro-Tip</em>: <code>line.split()</code> does the same and is more efficient than <code>line.split(" ")</code>. See <a href="http://stackoverflow.com/questions/38285654/why-is-str-strip-so-much-faster-than-str-strip">Why is str.strip() so much faster than str.strip(&#39; &#39;)?</a></p>

<hr>

<p>Essentially, your code becomes:</p>

<pre><code>with open("test.txt") as fp:
    for line in fp:
        l = line.split()
        print(l[1:].lstrip())
</code></pre>

python