Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53503 > unrolled thread
| Started by | MRAB <python@mrabarnett.plus.com> |
|---|---|
| First post | 2013-09-02 17:53 +0100 |
| Last post | 2013-09-02 17:53 +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.
Re: How can I remove the first line of a multi-line string? MRAB <python@mrabarnett.plus.com> - 2013-09-02 17:53 +0100
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-09-02 17:53 +0100 |
| Subject | Re: How can I remove the first line of a multi-line string? |
| Message-ID | <mailman.501.1378140794.19984.python-list@python.org> |
On 02/09/2013 17:12, Chris “Kwpolska” Warrick wrote:
> On Mon, Sep 2, 2013 at 6:06 PM, 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.
>>
>> Thanks,
>> Anthony
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
> Use split() and join() methods of strings, along with slicing. Like this:
>
> fullstring = """foo
> bar
> baz"""
>
> sansfirstline = '\n'.join(fullstring.split('\n')[1:])
>
> The last line does this:
> 1. fullstring.split('\n') turns it into a list of ['foo', 'bar', 'baz']
> 2. the [1:] slice removes the first element, making it ['bar', 'baz']
> 3. Finally, '\n'.join() turns the list into a string separated by
> newlines ("""bar
> baz""")
>
Another way is to use .partition:
>>> fullstring = """foo\nbar\nbaz"""
>>> fullstring.partition("\n")[2]
'bar\nbaz'
Back to top | Article view | comp.lang.python
csiph-web