Discuss / Python / 递归去空格

递归去空格

Topic source

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

魏苏龙嘎

#2 Created at ... [Delete] [Delete and Lock User]

这么写为什么字符串前面带空格输出结果还是前面带空格呢? 赋值s=' l ' 结果: l


  • 1

Reply