Discuss / Python / 作业-比特猫

作业-比特猫

Topic source
    def trim(s):
#循环去除字符的第一个空格,直到第一个不是空格
while s[:1]==' ':
    s=s[1:]
#循环去除字符的最后一个空格,直到最后一个不是空格
while s[-1:]==' ':
    s=s[:-1]
return s
S=input('输入一个前后带空格的字符串:')
print('去除前后空格后,字符串为:',trim(S))

  • 1

Reply