def trim(s):
'''首先判断该字符串是否为空,如果为空,就返回该字符串,
如果不为空的话,就判断字符串首尾字符是否为空,
如果为空,就使用递归再次调用该函数trim(),否则就返回该函数'''
if len(s) == 0:
return s
elif s[0] == ' ':
return (trim(s[1:]))
elif s[-1] == ' ':
return (trim(s[:-1]))
return s
if trim('hello ') != 'hello':
print('测试失败!')
elif trim(' hello') != 'hello':
print('测试失败!')
elif trim(' hello ') != 'hello':
print('测试失败!')
elif trim(' hello world ') != 'hello world':
print('测试失败!')
elif trim('') != '':
print('测试失败!')
elif trim(' ') != '':
print('测试失败!')
else:
print('测试成功!')