Last Updated: September 09, 2019
·
4.908K
· simaofreitas

Recursive reverse string in Python

This is a way to reverse a string in a recursive manner using Python.

def reverseString(x):
    if x == "":
        return ""
    else:
        return aStr[len(x)-1] + reverseString(x[:len(x)-1])

You can, of course, declare the length of the string and store it in a variable. That would be good if it bothers you to call "len" twice on each iteration. It can be a problem if you're trying to reverse a book...

1 Response
Add your response

def reverseString(x):
if x == "":
pass
else:
return x[::-1]

print reverseString("Hello")

over 1 year ago ·