Discuss / Python / 交作业,这样很简洁

交作业,这样很简洁

Topic source

YIUCHIMAN

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

    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

  • 1

Reply