用切片操作,实现一个trim()函数,去除字符串首尾的空格。

s = 'hello '

def trim(s):
  return s

if trim(s[0]) == ' ':
  print(s[1:])
elif trim(s[-1]) == ' ':
  print(s[:-1])
else:
  print('格式正确')

猜你喜欢

转载自www.cnblogs.com/diandianchao/p/10028508.html