Str.lstrip () unexpected behavior

I am working on a scrapy project and am trying to parse the config

String attr_title

I need to remove "attr_" and get it title

. I have used lstrip ('attr_') but I am getting unexpected results. I know I lstrip

execute combinations and remove them, but I have a hard time figuring it out.

In [17]: "attr.title".lstrip('attr.')
Out[17]: 'itle'

      

PS: I know there are several solutions for extracting a string. I am interested in understanding this.

+3


source to share


2 answers


lstrip

iterates

across the result string until there is no more combination that matches the largest character set

Below is a small illustration.



In [1]: "attr.title".lstrip('attr.')
Out[1]: 'itle'  # Flow --> "attr." --> "t" --> Next char is 'i' which does not match any combination hence, iteration stops & end result ('itle') is returned

In [2]: "attr.tritle".lstrip('attr.')
Out[2]: 'itle' # "attr." --> "t" --> "r" --> Next char is 'i' which does not match any combination hence, iteration stops & end result ('itle') is returned

In [5]: "attr.itratitle".lstrip('attr.')
Out[5]: 'itratitle' # "attr." --> Next char is 'i' which does not match any combination hence, iteration stops & end result ('itratitle') is returned

      

+9


source


"attr.title".lstrip('attr.')

      

means "to remove all the a

, t

, t

, r

, .

on the left of this line until you see this symbol.



Then he takes a given string and removes a

two t

s, r

, .

and t

, since they are all contained in the template.

In i

it stops because it is not contained in the template.

+5


source







All Articles