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

113-模拟字符串rstrip用法

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

思路参考 112-模拟字符串lstrip用法

whitesps = ' \r\n\v\f\t'

def rmrsps(astr):
    for i in range(-1, -len(astr), -1):  # 自右向左,下示为负
        if astr[i] not in whitesps:
            return astr[:i + 1]  # 结束下标对应的字符不包含,所以加1
    else:
        return ''

if __name__ == '__main__':
    print(rmrsps(''))
    print(rmrsps('  \thello  '))

相关文章

网友评论

    本文标题:113-模拟字符串rstrip用法

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