Python打卡Day014-Day15——切片学习 # 方法一 def trim(s): length = len(s) if length > 0: for i in range(length): if s[i] != ' ': break j = length-1 while s[j] == ' ' and j >= i: j -= 1 s = s[i:j+1] print(s) # 方法二 def trim2(s): if s[:1] != ' ' and s[-1:] != ' ': return s elif s[:1] == ' ': return trim(s[1:]) else: return trim(s[:-1]) if __name__ == "__main__": trim(" dd sf ") trim2(" ukfdj nscidj ") s = "swlzwwanaa" list = list(s) l1 = list[::] print('l1:', l1) l2 = list[:2] print('l2:', l2) l3 = list[::-2] print('l3:', l3) l4 = list[-1:4:-1] print('l4:', l4) l5 = list[::2] print('l5:', l5) l6 = list[:4] print('l6:', l6) l7 = list[-1:4:-1] print('l7:', l7) l8 = list[::-1] print('l8:', l8)py