[issue39939] Add str methods to remove prefixes or suffixes

Dennis Sweeney report at bugs.python.org
Thu Mar 12 12:37:24 EDT 2020


Dennis Sweeney <sweeney.dennis650 at gmail.com> added the comment:

Yes:

    >>> x = "A"*10**6
    >>> x.cutprefix("B") is x
    True
    >>> x.cutprefix("") is x
    True

    >>> y = b"A"*10**6
    >>> y.cutprefix(b"B") is y
    True
    >>> y.cutprefix(b"") is y
    True

    >>> z = bytearray(b"A")*10**6
    >>> z.cutprefix(b"B") is z
    False
    >>> z.cutprefix(b"") is z
    False

I'm not sure whether this should be part of the spec or an implementation detail. The (str/bytes).replace method docs don't clarify this, but they have the same behavior:

    >>> x = "A"*10**6
    >>> x.replace("B", "C") is x
    True
    >>> x.replace("", "") is x
    True

    >>> y = b"A"*10**6
    >>> y.replace(b"B", b"C") is y
    True
    >>> y.replace(b"", b"") is y
    True

    >>> z = bytearray(b"A")*10**6
    >>> z.replace(b"B", b"C") is z
    False
    >>> z.replace(b"", b"") is z
    False

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39939>
_______________________________________


More information about the Python-bugs-list mailing list