File tree 1 file changed +33
-0
lines changed
1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
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 ]
You can’t perform that action at this time.
0 commit comments