TheAlgorithms-Python

Форк
0
33 строки · 837.0 Байт
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]
34

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.