Trying to understand this code for "Reverse"


#1


https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096


This code works correctly as I saw it on another post. I have been trying to analyze why line 4 makes the code work but I am not certain why. If I remove the "-c" from the brackets the function will return the last letter of the word repeated the length of the original string. Can someone explain what is happening here in line 4?


def reverse(text):
    rev = ""
    for c in range(len(text)):
        rev += text[len(text)-1-c]
    return rev


#2

the best way to understand is to see by using print:

def reverse(text):
    rev = ""
    for c in range(len(text)):
        print c
        rev += text[len(text)-1-c]
    return rev
print reverse("Python!")

c contains numbers which we use to go from right to left over the string


#3

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