Discuss / Python / 切片作业

切片作业

Topic source

状元刚01

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding: utf-8 -*-
def trim(s):
    if s=='':
      return s
      
    else:
      while s[:1]==' ': #用s[:1]代替s[0]可以避免s为空集而索引超出出错的情况;
        s=trim(s[1:])
        
      while s[-1:]==' ': #用s[-1:]代替s[-1]可以避免s为空集而索引超出出错的情况;
        s=trim(s[:-1])
      return s

  • 1

Reply