lstrip problem - beginner question

mstagliamonte madmaxthc at yahoo.it
Tue Jun 4 11:53:11 EDT 2013


On Tuesday, June 4, 2013 11:48:55 AM UTC-4, MRAB wrote:
> 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.

Hey,

Great! Now I understand!
So, basically, it is also stripping the numbers after the '_' !!

Thank you, I know a bit more now!

Have a nice day everyone :)
Max



More information about the Python-list mailing list