Reverse a string in Python

前端 未结 28 2480
南旧
南旧 2020-11-21 04:41

There is no built in reverse function for Python\'s str object. What is the best way of implementing this method?

If supplying a very conci

28条回答
  •  被撕碎了的回忆
    2020-11-21 05:08

    Here is simply:

    print "loremipsum"[-1::-1]

    and some logically:

    def str_reverse_fun():
        empty_list = []
        new_str = 'loremipsum'
        index = len(new_str)
        while index:
            index = index - 1
            empty_list.append(new_str[index])
        return ''.join(empty_list)
    print str_reverse_fun()
    

    output:

    muspimerol

提交回复
热议问题