Discuss / Python / 泷月

泷月

Topic source

    while(s[:1] == ' '):

        s = s[1:]

    while(s[-1:] == ' '):

        s = s[:-1]

    return s

厉害,这么简练,一下就通过了

Iam楚狂人

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

问你一下:你说空格是只占一个格吗?

Kingfyyyyy

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

他这个写的真的可以

这个while用的妙啊

KING_华少

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

不错不错,可以的

yqd2005

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

请问用while和if有区别么

流浪小行

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

但是多次使用了切片,效率上不会太好

为什么这里的while不需要计数呀,比如i = i+1这样子的,难道是因为多个空格只占一个位置吗?

卟帥卟帥

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

可以把括号去掉

def trim(s):
    while s[:1]==' ':
        s=s[1:]
    while s[-1:]==' ':
        s=s[:-1]
    return s

  • 1
  • 2

Reply