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


Groups > comp.lang.python > #46924

Re: lstrip problem - beginner question

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'argument': 0.05; 'beginner': 0.05; 'string': 0.09; 'way:': 0.09; 'subject:question': 0.10; 'python': 0.11; 'language.': 0.14; "'a',": 0.16; "'c',": 0.16; "'d',": 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'missing?': 0.16; 'subject:beginner': 0.16; 'surprises': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'everyone,': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'headers': 0.24; 'removed.': 0.24; 'simpler': 0.24; 'string,': 0.24; 'subject:problem': 0.24; 'header': 0.24; 'file.': 0.24; 'script': 0.25; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'characters': 0.30; 'sets': 0.30; 'strip': 0.31; 'such.': 0.31; 'text': 0.33; 'something': 0.35; 'should': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'numbers': 0.61; 'such': 0.63; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'happening?': 0.84; 'reply- to:addr:python.org': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=KrN0hwmN c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=oyR3mlnJdzkA:10 a=JCJ9wb98zREA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=_5o13rfjvDMA:10 a=TUod_fgIGRTk15JOd7kA:9 a=wPNLvfGTeEIA:10
X-AUTH mrabarnett:2500
Date Tue, 04 Jun 2013 16:48:55 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130509 Thunderbird/17.0.6
MIME-Version 1.0
To python-list@python.org
Subject Re: lstrip problem - beginner question
References <1829efca-935d-4049-ba61-7138015a2806@googlegroups.com>
In-Reply-To <1829efca-935d-4049-ba61-7138015a2806@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2658.1370360923.3114.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1370360924 news.xs4all.nl 15992 [2001:888:2000:d::a6]:37133
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:46924

Show key headers only | View raw


On 04/06/2013 16:21, mstagliamonte wrote:
> Hi everyone,
>
> I am a beginner in python and trying to find my way through... :)
>
> I am writing a script to get numbers from the headers of a text file.
>
> If the header is something like:
> h01 = ('>scaffold_1')
> I just use:
> h01.lstrip('>scaffold_')
> and this returns me '1'
>
> But, if the header is:
> h02: ('>contig-100_0')
> if I use:
> h02.lstrip('>contig-100_')
> this returns me with: ''
> ...basically nothing. What surprises me is that if I do in this other way:
> h02b = h02.lstrip('>contig-100')
> I get h02b = ('_1')
> and subsequently:
> h02b.lstrip('_')
> returns me with: '1' which is what I wanted!
>
> Why is this happening? What am I missing?
>
The methods 'lstrip', 'rstrip' and 'strip' don't strip a string, they
strip characters.

You should think of the argument as a set of characters to be removed.

This code:

h01.lstrip('>scaffold_')

will return the result of stripping the characters '>', '_', 'a', 'c',
'd', 'f', 'l', 'o' and 's' from the left-hand end of h01.

A simpler example:

 >>> 'xyyxyabc'.lstrip('xy')
'abc'

It strips the characters 'x' and 'y' from the string, not the string
'xy' as such.

They are that way because they have been in Python for a long time,
long before sets and such like were added to the language.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:21 -0700
  Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:24 -0700
  Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:25 -0700
    Re: lstrip problem - beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 16:41 +0100
      Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:49 -0700
        Re: lstrip problem - beginner question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-06-04 17:01 +0100
        Re: lstrip problem - beginner question Dave Angel <davea@davea.name> - 2013-06-04 17:48 -0400
  Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:28 -0700
  Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:29 -0700
  Re: lstrip problem - beginner question MRAB <python@mrabarnett.plus.com> - 2013-06-04 16:48 +0100
    Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:53 -0700
  Re: lstrip problem - beginner question Peter Otten <__peter__@web.de> - 2013-06-04 17:52 +0200
  Re: lstrip problem - beginner question John Gordon <gordon@panix.com> - 2013-06-04 15:55 +0000
    Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 09:06 -0700
  Re: lstrip problem - beginner question Larry Hudson <orgnut@yahoo.com> - 2013-06-04 21:53 -0700

csiph-web