python用递归方式去掉首尾空格

# 递归去除空格
def trip_str(s):
    if s[0] == ' ':
        return trip_str(s[1:])
    elif s[-1] == " ":
        return trip_str(s[:-1])
    else:
        return s
print(trip_str("a  b     "))
print(trip_str("   a  b     "))
print(trip_str("1   a  b     "))

posted @ 2023-03-16 16:49  dyjnicole  阅读(17)  评论(0编辑  收藏  举报