[Python-Dev] str.lstrip bug?

Ethan Furman ethan at stoneleaf.us
Tue Mar 10 20:12:40 CET 2015


On 03/10/2015 10:57 AM, Isaac Schwabacher wrote:
> On 15-03-10, Facundo Batista  wrote:
>> On Tue, Mar 10, 2015 at 2:27 PM, lou xiao wrote:
>>
>>> tiny➜ ~ python
>>> Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
>>> [GCC 4.8.1] on linux2
>>> Type "help", "copyright", "credits" or "license" for more information.
>>> >>> a='device_info'
>>> >>> a.lstrip('device')
>>> '_info'
>>> >>> a.lstrip('device_')
>>> 'nfo'
>>
>> On one hand, this is the "development of python itself" list; this
>> issue was more aimed to the general python list, of you was sure that
>> this is a real bug, to the issue tracker.
>>
>> On the other hand, this is not a bug! If you pass a parameter to
>> lstrip it will (quoted from its help) "remove characters in chars
>> instead.", so the moment you pass "device_", it removes all those
>> characers from the left... note that the 'i' is removed twice.
> 
> That said, I bet this is the most common string-munging operations that *isn't* available as a single function in the stdlib. I know my bash code is full of ${filename%.suffix} and such, and the fact that in python I have to either import re or resort to some combination of (starts|ends)with, r?partition, slicing and an if-clause makes that sort of code much more verbose and harder to read. Pathlib's Path.with_suffix helps in some but not all of these cases. Maybe the stdlib should have a simple way to do this? It could even be added as a kwarg (exact=False) to str.[lr]strip to minimize the effect on the API; alternatively it could be str.strip(prefix|suffix).

Nice way to bring it on-topic!

And yes, that would be nice.

--
~Ethan~

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150310/7676701e/attachment.sig>


More information about the Python-Dev mailing list