while s[:1]==" ":
s=s[1:]
while s[-1:]==" ":
s=s[:-2]
return s
第二个循环里应该是s=s[:-1]吧,-2的话少判断了一位?
s=s[:-2]才去掉最后的空格啊
是s = s[:-1],-2直接干掉了两位,包前不包后
def trim(s): while True: if s.startswith(" "): s = s[1:] elif s.endswith(" "): s = s[:-1] else: break return s
Sign in to make a reply
YIUCHIMAN
while s[:1]==" ":
s=s[1:]
while s[-1:]==" ":
s=s[:-2]
return s