def trim(s): if s[:1] != ' ' and s[-1:] != ' ': return s elif s[:1] == ' ': return trim(s[1:]) else : return trim(s[:-1]) print(trim(" hello ")) print(" hello ")
实现一个trim()函数,去除字符串首尾的空格
最新推荐文章于 2025-02-22 09:10:18 发布
def trim(s): if s[:1] != ' ' and s[-1:] != ' ': return s elif s[:1] == ' ': return trim(s[1:]) else : return trim(s[:-1]) print(trim(" hello ")) print(" hello ")