This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.daprano
Recipients opensource-assist, rhettinger, steven.daprano, xtreak
Date 2020-01-11.15:57:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1578758274.27.0.45428107863.issue39304@roundup.psfhosted.org>
In-reply-to
Content
This behaviour that goes all the way back to Python 1.5, if not older, before strings even had methods:

    [steve@ando ~]$ python1.5
    Python 1.5.2 (#1, Aug 27 2012, 09:09:18)  [GCC 4.1.2 20080704 
    (Red Hat 4.1.2-52)] on linux2
    Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
    >>> import string
    >>> string.replace("abacadaeaf", "a", "Z", -1)
    'ZbZcZdZeZf'


Hiding the fact that str.replace treats negative values as "replace all" just causes confusion, as people wrongly jump to the conclusion that it is a bug.

It's not a bug, it is a useful feature and it has been in the language for over 20 years. VB.Net has the same feature:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.strings.replace?view=netframework-4.8

Let's just document it as intentional and be done with it.
History
Date User Action Args
2020-01-11 15:57:54steven.dapranosetrecipients: + steven.daprano, rhettinger, xtreak, opensource-assist
2020-01-11 15:57:54steven.dapranosetmessageid: <1578758274.27.0.45428107863.issue39304@roundup.psfhosted.org>
2020-01-11 15:57:54steven.dapranolinkissue39304 messages
2020-01-11 15:57:53steven.dapranocreate