Python reversing a string using recursion

前端 未结 6 625
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 23:34

I want to use recursion to reverse a string in python so it displays the characters backwards (i.e \"Hello\" will become \"olleh\"/\"o l l e h\".

I wrote one that do

6条回答
  •  温柔的废话
    2020-12-15 00:08

    def rreverse(s):
        if s == "":
            return s
        else:
            return rreverse(s[1:]) + s[0]
    

    (Very few people do heavy recursive processing in Python, the language wasn't designed for it.)

提交回复
热议问题