4

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

The string is attr_title I have to strip 'attr_' and get title. I used lstrip('attr_'), but getting unexpected results. I know lstrip works out combinations and removes them, but having hard time understanding it.

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

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

geetha ar
  • 136
  • 5
  • 2
    related: [Python - how to remove the left part of a string?](http://stackoverflow.com/q/599953/4279) – jfs Jul 18 '15 at 07:35

2 Answers2

9

lstrip iterates over the result string until there is no more combination that matches the left most set of characters

A little illustration is below.

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
kumar
  • 2,570
  • 2
  • 17
  • 18
  • Thanks for explaining, and for the example illustrations. That helped understand even better. – geetha ar Jul 18 '15 at 07:29
  • 3
    There is nothing recursive about this. – Tim Pietzcker Jul 18 '15 at 07:37
  • 2
    Recursively must be taken with grammatical context... As OP was having hard time understanding I explained it further. – kumar Jul 18 '15 at 07:42
  • As Tim say, the `strip` methods do **not** operate recursively - they use a simple `while` loop. Your explanation would be more accurate if you said "`lstrip` operates iteratively on the input string". See [the source code](http://svn.python.org/view/python/trunk/Objects/stringobject.c?view=markup) for `do_argstrip()` (starting at line #1882) for details. – PM 2Ring Jul 18 '15 at 07:44
5
"attr.title".lstrip('attr.')

means "remove every a, t, t, r, . from the left of the given string until no such character occurs.

Then it takes the given string and removes the a, the two ts, the r, the . and the t, as they all are contained in the given pattern.

At the i it stops as it is not contained in the pattern.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • Thanks for explaining, ahhh, now I get it. – geetha ar Jul 18 '15 at 07:28
  • 1
    @geethaar: unfortunately, unlike `.startswith()` method `.lstrip()` does not accept a `tuple()` argument (to strip whole strings instead of individual characters) – jfs Jul 18 '15 at 07:37