def trim(s):
if (not s) or (s[0]!=' ' and s[-1]!=' '): #字符串为空或者左右都无空字符
return s
if s[0]==' ': #如果左侧空
return trim(s[1:])
if s[-1]==' ': #如果右侧空
return trim(s[:-1])
说错了,是递归,不是迭代
Sign in to make a reply
Kris-As-A-Service
def trim(s):
if (not s) or (s[0]!=' ' and s[-1]!=' '): #字符串为空或者左右都无空字符
return s
if s[0]==' ': #如果左侧空
return trim(s[1:])
if s[-1]==' ': #如果右侧空
return trim(s[:-1])