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...
Written by Simão Freitas
Related protips
1 Response
def reverseString(x):
if x == "":
pass
else:
return x[::-1]
print reverseString("Hello")
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#