We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a12b07f commit e89ae55Copy full SHA for e89ae55
strings/strip.py
@@ -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
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