I'm trying to make a program that spells an entered word in reverse. I finally got it to work, but I don't understand why it works. Here is the working program:
def reverse(text):
length = len(text)
text = list(text)
new = ["None"] * length
for i in range(0, length):
new[i] = text[length - i - 1]
return "".join(new)
return new
Shouldn't making i
range(0, length)
be one character longer than the original word? Ex. if the original word is four characters, range(0, length)
would be five characters, wouldn't it? Seems like the range should be (0, length - 1)
, but that didn't work! Could someone please explain why?