- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathstrip.py
33 lines (26 loc) · 837 Bytes
/
strip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
defstrip(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)
whilestart<endanduser_string[start] incharacters:
start+=1
whileend>startanduser_string[end-1] incharacters:
end-=1
returnuser_string[start:end]