Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It doesn't replace the `lstrip` or `rstrip` methods; it replaces the need for an `if` statement using `startswith` and `endswith` methods.

    if "foobar".startswith("foo"): "foobar"[len("foo"):]


The parent comment referred to bugs from use of lstrip and rstrip for this purpose. The problem is that they often appear to work for this job, so they can make it all the way into code. Your own example strings illustrate that:

   > "foobar".lstrip("foo")
   "bar"
Of course, if the developer making this mistake tested with "foofbar" instead then they would realise their mistake. A more typical example of this type of mistake would be url.lstrip("http://").


If the developer is using str.lstrip or str.rstrip, they're doing it wrong (because they're using the wrong method).

    > "foobar".lstrip("wooowweeezoweeef")
    bar


Yes, that's what both comments you replied to have said.


That always bites people. `lstrip` and `rstrip` take a set of characters to be removed not a prefix/suffix.

    >>> "foobarbaz".lstrip("fo")
    'barbaz'




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: