Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create strip.py #10011

Merged
merged 2 commits into from
Oct 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions strings/strip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def strip(user_string: str, characters: str = " \t\n\r") -> 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'
>>> strip("")
''
"""

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]