0

I´m trying to make a string reverse function in Python. However, I won´t use "reversed" or "[::-1]". I´m still a beginner in Python. So, I´ve made this function reverse, which iterates trough each letter in a list "text"(I´ve converted the string "text" into a list.). I have a second list called "result", that is equal to the original "text" list. And when I iterate through the "text" list, I want to replace that letter with letters in reverse order of the result variable. However, my function seems not only to replace the letters with the "result" variable but also with the "text" variable. I can´t figure it out why does the function change the "text" variable. (All those print function are used to debug)

def reverse(text):
    text= list(text)
    counting= 0
    result=text
    for letter in text:
        print(text)
        print(letter)
        counting = counting + 1
        result[len(text)-counting]=letter
        print(result)
    return(result)

reverse("United")

Abhi V
  • 684
  • 1
  • 4
  • 19
mwmouawad
  • 33
  • 1
  • 4

0 Answers0