欢迎使用皮肤 Geek|

园龄:粉丝:关注:

python切片索引循环每次去掉最后一个字符

class Course9:
    def Test1(self=0):
        word = "This's a test"
        # print(type(word))
        i = len(word)
        while i > 0:
            print(word[0:i - 1])
            i -= 1

    def Test2(self=None):
        word = "This's a test"
        # print(type(word))
        for i in [None] + list(range(-1, -len(word), -1)):
            # print(i)
            print(word[:i])

    def Test3(self=None):
        word = "This's a test"
        # print(type(word))
        for i in  list(range(0, len(word), 1)):
            # print(i)
            print(word[:-i] if i > 0 else word[0:])


if __name__ == '__main__':
    # Course9.Test1()
    Course9.Test3()

写了三种方式循环输出每次去掉最后一个字符,新手可以借鉴一下。

本文作者:xxxyz

本文链接:https://www.cnblogs.com/xxxyz/p/16543994.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   xxxyz  阅读(128)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起