首页 > 编程语言 >python用递归方式去掉首尾空格

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

时间:2023-03-16 16:55:23浏览次数:46  
标签:return 递归 python 空格 str print trip

# 递归去除空格
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     "))

标签:return,递归,python,空格,str,print,trip
From: https://www.cnblogs.com/dyjnicole/p/17223278.html

相关文章