Reverse a string in Python

前端 未结 28 2469
南旧
南旧 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

    Recursive method:

    def reverse(s): return s[0] if len(s)==1 else s[len(s)-1] + reverse(s[0:len(s)-1])
    

    example:

    print(reverse("Hello!"))    #!olleH
    

提交回复
热议问题