美文网首页python百例
112-模拟字符串lstrip用法

112-模拟字符串lstrip用法

作者: 凯茜的老爸 | 来源:发表于2018-08-26 23:22 被阅读49次

    思路:
    1、取出字符串长度
    2、通过range和字符串长度得到字符串下标
    3、找到非空字符串下标,剩余部分取切片
    4、如果字符串没有非空字符,返回空串

    whitesps = ' \r\n\v\f\t'
    
    def rmlsps(astr):
        for i in range(len(astr)):
            if astr[i] not in whitesps:
                return astr[i:]
        else:  # 所有字符均为空,循环正常结束,返回空串
            return ''
    
    if __name__ == '__main__':
        print(rmlsps('  \thello  '))
    

    相关文章

      网友评论

        本文标题:112-模拟字符串lstrip用法

        本文链接:https://www.haomeiwen.com/subject/fqmaiftx.html