diff options
author | sweeneyde <36520290+sweeneyde@users.noreply.github.com> | 2020-04-22 17:05:48 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-22 23:05:48 +0200 |
commit | a81849b0315277bb3937271174aaaa5059c0b445 (patch) | |
tree | 8184e6ab012aa217b5cc1bb242efc6aed531db7d /Lib/collections | |
parent | bpo-40260: Remove unnecessary newline in compile() call (GH-19641) (diff) | |
download | cpython-a81849b0315277bb3937271174aaaa5059c0b445.tar.gz cpython-a81849b0315277bb3937271174aaaa5059c0b445.tar.bz2 cpython-a81849b0315277bb3937271174aaaa5059c0b445.zip |
bpo-39939: Add str.removeprefix and str.removesuffix (GH-18939)
Added str.removeprefix and str.removesuffix methods and corresponding
bytes, bytearray, and collections.UserString methods to remove affixes
from a string if present. See PEP 616 for a full description.
Diffstat (limited to 'Lib/collections')
-rw-r--r-- | Lib/collections/__init__.py | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index e19840650dd..bb9a605a994 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -1239,6 +1239,14 @@ class UserString(_collections_abc.Sequence): if isinstance(sub, UserString): sub = sub.data return self.data.count(sub, start, end) + def removeprefix(self, prefix, /): + if isinstance(prefix, UserString): + prefix = prefix.data + return self.__class__(self.data.removeprefix(prefix)) + def removesuffix(self, suffix, /): + if isinstance(suffix, UserString): + suffix = suffix.data + return self.__class__(self.data.removesuffix(suffix)) def encode(self, encoding='utf-8', errors='strict'): encoding = 'utf-8' if encoding is None else encoding errors = 'strict' if errors is None else errors |