利用切片操作,实现一个trim()函数,去除字符串首尾的空格,不调用str的strip()方法:

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
发布了13 篇原创文章 · 获赞 0 · 访问量 85

猜你喜欢

转载自blog.csdn.net/weixin_42692164/article/details/104815209
今日推荐