def trim(s):
if len(s)==0:
return s
while s[0]==' ':
s=s[1:]
if len(s)==0:
return s
while s[-1] == ' ':
s=s[:-1]
if len(s)==0:
return s
return s
利用切片操作,实现一个trim()函数,去除字符串首尾的空格,不调用str的strip()方法:
猜你喜欢
转载自blog.csdn.net/weixin_42692164/article/details/104815209
今日推荐
周排行