Discuss / Python / 通过递归的方法循环检查首尾是否为空,如果不为空,返回不为空的s

通过递归的方法循环检查首尾是否为空,如果不为空,返回不为空的s

Topic source

def trim(s):

    if len(s) > 0:

        if s[0] == '  ':

            return trim(s[1:])

        elif s[-1] == '  ':

            return trim(s[:-1])

        else:

            return s

    else:

        return s


  • 1

Reply