Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

reverse a string python

# Recursive
def reverse(string):
    if len(string) == 0:
        return string
    else:
        return reverse(string[1:]) + string[0]

# Iterative
def reverse_iter(string):
    if len(string) == 0:
        return string
    else:
        new_string = ""
        for i in range(len(string)):
            new_string += string[len(string)-i-1]
        return new_string
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #reverse #string #python
ADD COMMENT
Topic
Name
6+1 =