Q:

Python returns IndexError: string index out of range on this funtion why

I don't understand why this code doesn't work:
def reverse(string):
    rev_string = []
    for c in range(len(string)):
        rev_string[c]=string[(len(string))-c]
    str(rev_string)
    return rev_string
print(reverse("google"))

and throws this error message:

IndexError: string index out of range

A:

There are three problems in your code. 

rev_string is an empty list you can not index it, you can use append instead; 
you need to use ''.join instead of str to make a string from a list;
list index is 0 based, so max index is len(string) - 1:

def reverse(string):

    rev_string = []
    for c in range(len(string)):
        rev_string.append(string[len(string)-c-1])
​
    return ''.join(rev_string)

print(reverse("google"))
# elgoog

