From 729c3c02a43c0490a16908c9559f1f0551bd5ddd Mon Sep 17 00:00:00 2001 From: Saurabh Mahapatra <98408932+its-100rabh@users.noreply.github.com> Date: Sat, 7 Oct 2023 18:09:54 +0530 Subject: [PATCH 1/2] Create strip.py --- strings/strip.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 strings/strip.py diff --git a/strings/strip.py b/strings/strip.py new file mode 100644 index 000000000000..f6079a246ba6 --- /dev/null +++ b/strings/strip.py @@ -0,0 +1,33 @@ +def strip(user_string: str, characters: str | None = None) -> str: + """ + Remove leading and trailing characters (whitespace by default) from a string. + + Args: + user_string (str): The input string to be stripped. + characters (str, optional): Optional characters to be removed + (default is whitespace). + + Returns: + str: The stripped string. + + Examples: + >>> strip(" hello ") + 'hello' + >>> strip("...world...", ".") + 'world' + >>> strip("123hello123", "123") + 'hello' + """ + if characters is None: + characters = " \t\n\r" + + start = 0 + end = len(user_string) + + while start < end and user_string[start] in characters: + start += 1 + + while end > start and user_string[end - 1] in characters: + end -= 1 + + return user_string[start:end] From 0260fb0a741f16d59ced0954c89e66d8e0bf0227 Mon Sep 17 00:00:00 2001 From: Saurabh Mahapatra <98408932+its-100rabh@users.noreply.github.com> Date: Sun, 8 Oct 2023 13:37:43 +0530 Subject: [PATCH 2/2] Update strip.py --- strings/strip.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/strings/strip.py b/strings/strip.py index f6079a246ba6..d4f901f0c7ea 100644 --- a/strings/strip.py +++ b/strings/strip.py @@ -1,4 +1,4 @@ -def strip(user_string: str, characters: str | None = None) -> str: +def strip(user_string: str, characters: str = " \t\n\r") -> str: """ Remove leading and trailing characters (whitespace by default) from a string. @@ -17,9 +17,9 @@ def strip(user_string: str, characters: str | None = None) -> str: 'world' >>> strip("123hello123", "123") 'hello' + >>> strip("") + '' """ - if characters is None: - characters = " \t\n\r" start = 0 end = len(user_string)