Discuss / Python / 解决问题,虽然写的很菜

解决问题,虽然写的很菜

Topic source

```python

def trim(s):

    l = len(s)

    rs = ''

    start_lock = True

    end_lock = True

    if s[0] == ' ':

        for i in range(l):

            if s[i] != ' ' and start_lock:

                rs = s[i:]

                start_lock = False

                return trim(rs)

    elif s[-1] == ' ':

        for i in range(l):

            if s[-i - 1] != ' ' and end_lock:

                rs = s[:-i]

                end_lock = False

                return rs

    else:

        return s

s = trim(' afbc cd fef')

print(s,len(s)) # afbc cd fef 11

```


  • 1

Reply