TheAlgorithms-Python
33 строки · 837.0 Байт
1def strip(user_string: str, characters: str = " \t\n\r") -> str:2"""3Remove leading and trailing characters (whitespace by default) from a string.
4
5Args:
6user_string (str): The input string to be stripped.
7characters (str, optional): Optional characters to be removed
8(default is whitespace).
9
10Returns:
11str: The stripped string.
12
13Examples:
14>>> strip(" hello ")
15'hello'
16>>> strip("...world...", ".")
17'world'
18>>> strip("123hello123", "123")
19'hello'
20>>> strip("")
21''
22"""
23
24start = 025end = len(user_string)26
27while start < end and user_string[start] in characters:28start += 129
30while end > start and user_string[end - 1] in characters:31end -= 132
33return user_string[start:end]34