Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53501 > unrolled thread

Re: How can I remove the first line of a multi-line string?

Started byOscar Benjamin <oscar.j.benjamin@gmail.com>
First post2013-09-02 17:18 +0100
Last post2013-09-02 17:18 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: How can I remove the first line of a multi-line string? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-09-02 17:18 +0100

#53501 — Re: How can I remove the first line of a multi-line string?

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-09-02 17:18 +0100
SubjectRe: How can I remove the first line of a multi-line string?
Message-ID<mailman.499.1378138729.19984.python-list@python.org>
On 2 September 2013 17:06, Anthony Papillion <papillion@gmail.com> wrote:
> Hello Everyone,
>
> I have a multi-line string and I need to remove the very first line from
> it. How can I do that? I looked at StringIO but I can't seem to figure
> out how to properly use it to remove the first line. Basically, I want
> to toss the first line but keep everything else.  Can anyone put me on
> the right path? I know it is probably easy but I'm still learning Python
> and don't have all the string functions down yet.

>>> a = '''asd
... qwe
... rty'''
>>> print(a)
asd
qwe
rty
>>> a.splitlines()
['asd', 'qwe', 'rty']
>>> a.splitlines(True)  # Keep the new lines
['asd\n', 'qwe\n', 'rty']
>>> a.splitlines(True)[1:]  # remove first line
['qwe\n', 'rty']
>>> ''.join(a.splitlines(True)[1:]) # recombine
'qwe\nrty'
>>> a = ''.join(a.splitlines(True)[1:])
>>> print(a)
qwe
rty


Oscar

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web