4 ms·
It doesn't replace the `lstrip` or `rstrip` methods; it replaces the need for an `if` statement using `startswith` and `endswith` methods. if "foobar".star
by doc_gunthrop 6y ago
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"):]
- deleted 6y ago[deleted]
- quietbritishjim 6y agoThe 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:// http://").
- doc_gunthrop 6y agoIf 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
- vultour 6y agoYes, that's what both comments you replied to have said.
- digianarchist 6y agoThat always bites people. `lstrip` and `rstrip` take a set of characters to be removed not a prefix/suffix. >>> "foobarbaz".lstrip("fo") 'barbaz'