Reverse (using range with -1 step)


#1

def reverse(text):
    # necessary to work with symbols and numbers as well as letters
    text = str(text)
    x = len(text)
    # creating an empty list for reverse string
    rev_text = []
    # range (start, stop, step) starting at length-1, to -1, using a -1 step (counts down)
    for i in range(x-1,-1, -1):
        rev_text.append(text[i])
    # returning as a string instead of a list (learned this trick in the battleship exercise)
    return "".join(rev_text)
    print rev_text

print reverse("abcd!?")

#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.