Skip to content

Commit e89ae55

Browse files
authored
Create strip.py (#10011)
* Create strip.py * Update strip.py
1 parent a12b07f commit e89ae55

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

strings/strip.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def strip(user_string: str, characters: str = " \t\n\r") -> str:
2+
"""
3+
Remove leading and trailing characters (whitespace by default) from a string.
4+
5+
Args:
6+
user_string (str): The input string to be stripped.
7+
characters (str, optional): Optional characters to be removed
8+
(default is whitespace).
9+
10+
Returns:
11+
str: The stripped string.
12+
13+
Examples:
14+
>>> strip(" hello ")
15+
'hello'
16+
>>> strip("...world...", ".")
17+
'world'
18+
>>> strip("123hello123", "123")
19+
'hello'
20+
>>> strip("")
21+
''
22+
"""
23+
24+
start = 0
25+
end = len(user_string)
26+
27+
while start < end and user_string[start] in characters:
28+
start += 1
29+
30+
while end > start and user_string[end - 1] in characters:
31+
end -= 1
32+
33+
return user_string[start:end]

0 commit comments

Comments
 (0)