Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-xxxfer.readnews.com!news-out.readnews.com!transit4.readnews.com!panix!gordon From: John Gordon Newsgroups: comp.lang.python Subject: Re: lstrip problem - beginner question Date: Tue, 4 Jun 2013 15:55:06 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 50 Message-ID: References: <1829efca-935d-4049-ba61-7138015a2806@googlegroups.com> NNTP-Posting-Host: panix1.panix.com X-Trace: reader1.panix.com 1370361306 7857 166.84.1.1 (4 Jun 2013 15:55:06 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Tue, 4 Jun 2013 15:55:06 +0000 (UTC) User-Agent: nn/6.7.3 Xref: csiph.com comp.lang.python:46929 In <1829efca-935d-4049-ba61-7138015a2806@googlegroups.com> mstagliamonte writes: > 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? It's happening because the argument you pass to lstrip() isn't an exact string to be removed; it's a set of individual characters, all of which will be stripped out. So, when you make this call: h02.lstrip('>contig-100_') You're telling python to remove all of the characters in '>contig-100_' from the base string, which leaves nothing remaining. The reason it "worked" on your first example was that the character '1' didn't occur in your sample header string 'scaffold_'. If the underscore character is always the separating point in your headers, a better way might be to use the split() method instead of lstrip(). -- John Gordon A is for Amy, who fell down the stairs gordon@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies"