@@ -1549,6 +1549,33 @@ expression support in the :mod:`re` module).
15491549 interpreted as in slice notation.
15501550
15511551
1552+ .. method :: str.removeprefix(prefix, /)
1553+
1554+ If the string starts with the *prefix * string, return
1555+ ``string[len(prefix):] ``. Otherwise, return a copy of the original
1556+ string::
1557+
1558+ >>> 'TestHook'.removeprefix('Test')
1559+ 'Hook'
1560+ >>> 'BaseTestCase'.removeprefix('Test')
1561+ 'BaseTestCase'
1562+
1563+ .. versionadded :: 3.9
1564+
1565+ .. method :: str.removesuffix(suffix, /)
1566+
1567+ If the string ends with the *suffix * string and that *suffix * is not empty,
1568+ return ``string[:-len(suffix)] ``. Otherwise, return a copy of the
1569+ original string::
1570+
1571+ >>> 'MiscTests'.removesuffix('Tests')
1572+ 'Misc'
1573+ >>> 'TmpDirMixin'.removesuffix('Tests')
1574+ 'TmpDirMixin'
1575+
1576+ .. versionadded :: 3.9
1577+
1578+
15521579.. method :: str.encode(encoding="utf-8", errors="strict")
15531580
15541581 Return an encoded version of the string as a bytes object. Default encoding
@@ -1831,6 +1858,14 @@ expression support in the :mod:`re` module).
18311858 >>> 'www.example.com'.lstrip('cmowz.')
18321859 'example.com'
18331860
1861+ See :meth: `str.removeprefix ` for a method that will remove a single prefix
1862+ string rather than all of a set of characters. For example::
1863+
1864+ >>> 'Arthur: three!'.lstrip('Arthur: ')
1865+ 'ee!'
1866+ >>> 'Arthur: three!'.removeprefix('Arthur: ')
1867+ 'three!'
1868+
18341869
18351870.. staticmethod :: str.maketrans(x[, y[, z]])
18361871
@@ -1911,6 +1946,13 @@ expression support in the :mod:`re` module).
19111946 >>> 'mississippi'.rstrip('ipz')
19121947 'mississ'
19131948
1949+ See :meth: `str.removesuffix ` for a method that will remove a single suffix
1950+ string rather than all of a set of characters. For example::
1951+
1952+ >>> 'Monty Python'.rstrip(' Python')
1953+ 'M'
1954+ >>> 'Monty Python'.removesuffix(' Python')
1955+ 'Monty'
19141956
19151957.. method :: str.split(sep=None, maxsplit=-1)
19161958
@@ -2591,6 +2633,50 @@ arbitrary binary data.
25912633 Also accept an integer in the range 0 to 255 as the subsequence.
25922634
25932635
2636+ .. method :: bytes.removeprefix(prefix, /)
2637+ bytearray.removeprefix(prefix, /)
2638+
2639+ If the binary data starts with the *prefix * string, return
2640+ ``bytes[len(prefix):] ``. Otherwise, return a copy of the original
2641+ binary data::
2642+
2643+ >>> b'TestHook'.removeprefix(b'Test')
2644+ b'Hook'
2645+ >>> b'BaseTestCase'.removeprefix(b'Test')
2646+ b'BaseTestCase'
2647+
2648+ The *prefix * may be any :term: `bytes-like object `.
2649+
2650+ .. note ::
2651+
2652+ The bytearray version of this method does *not * operate in place -
2653+ it always produces a new object, even if no changes were made.
2654+
2655+ .. versionadded :: 3.9
2656+
2657+
2658+ .. method :: bytes.removesuffix(suffix, /)
2659+ bytearray.removesuffix(suffix, /)
2660+
2661+ If the binary data ends with the *suffix * string and that *suffix * is
2662+ not empty, return ``bytes[:-len(suffix)] ``. Otherwise, return a copy of
2663+ the original binary data::
2664+
2665+ >>> b'MiscTests'.removesuffix(b'Tests')
2666+ b'Misc'
2667+ >>> b'TmpDirMixin'.removesuffix(b'Tests')
2668+ b'TmpDirMixin'
2669+
2670+ The *suffix * may be any :term: `bytes-like object `.
2671+
2672+ .. note ::
2673+
2674+ The bytearray version of this method does *not * operate in place -
2675+ it always produces a new object, even if no changes were made.
2676+
2677+ .. versionadded :: 3.9
2678+
2679+
25942680.. method :: bytes.decode(encoding="utf-8", errors="strict")
25952681 bytearray.decode(encoding="utf-8", errors="strict")
25962682
@@ -2841,7 +2927,14 @@ produce new objects.
28412927 b'example.com'
28422928
28432929 The binary sequence of byte values to remove may be any
2844- :term: `bytes-like object `.
2930+ :term: `bytes-like object `. See :meth: `~bytes.removeprefix ` for a method
2931+ that will remove a single prefix string rather than all of a set of
2932+ characters. For example::
2933+
2934+ >>> b'Arthur: three!'.lstrip(b'Arthur: ')
2935+ b'ee!'
2936+ >>> b'Arthur: three!'.removeprefix(b'Arthur: ')
2937+ b'three!'
28452938
28462939 .. note ::
28472940
@@ -2890,7 +2983,14 @@ produce new objects.
28902983 b'mississ'
28912984
28922985 The binary sequence of byte values to remove may be any
2893- :term: `bytes-like object `.
2986+ :term: `bytes-like object `. See :meth: `~bytes.removesuffix ` for a method
2987+ that will remove a single suffix string rather than all of a set of
2988+ characters. For example::
2989+
2990+ >>> b'Monty Python'.rstrip(b' Python')
2991+ b'M'
2992+ >>> b'Monty Python'.removesuffix(b' Python')
2993+ b'Monty'
28942994
28952995 .. note ::
28962996
0 commit comments