def trim(s):
if s =='':
return s
else:
while s!='' and s[0]==' ':
s=s[1:]
while s!='' and s[-1]==' ':
s=s[:-1]
另: 之所以是s[:-1]是因为
说明中有写 L[0:3]表示,从索引0开始取,直到索引3为止,但不包括索引3
L[0:3]
0
3
即:-1表示-1包括,到-2为止
-1不包括,写错了,hh
Sign in to make a reply
小贪想睡觉
def trim(s):
if s =='':
return s
else:
while s!='' and s[0]==' ':
s=s[1:]
while s!='' and s[-1]==' ':
s=s[:-1]
return s
另: 之所以是s[:-1]是因为
说明中有写
L[0:3]
表示,从索引0
开始取,直到索引3
为止,但不包括索引3
即:-1表示-1包括,到-2为止