def trim(s): if s == '': return s else: if s[0] == ' ' or s[-1] == ' ': # 首或尾有空格 if s[0] == ' ': # 首有空格 s = s[1:] if s[-1] == ' ': # 尾也有空格 s = s[:-1] else: # 仅尾有空格 s = s[:-1] return trim(s) # 使用递归,预防首尾有多个空格的情况 else: return s
写的好
运行失败
大佬,请收下我的膝盖
牛
Sign in to make a reply
Pace2cool