MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / strip

Function strip

strings/strip.py:1–33  ·  view source on GitHub ↗

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 stri

(user_string: str, characters: str = " \t\n\r")

Source from the content-addressed store, hash-verified

1def 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]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected