Reverse a string in Python

前端 未结 28 2560
南旧
南旧 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:03

    Here is a no fancy one:

    def reverse(text):
        r_text = ''
        index = len(text) - 1
    
        while index >= 0:
            r_text += text[index] #string canbe concatenated
            index -= 1
    
        return r_text
    
    print reverse("hello, world!")
    

提交回复
热议问题