You need to start at indexing your string from -1 through to -stringsize, and use empty.append() to add values:
for x in range(stringsize):
empty.append(c[stringsize - x - 1])
Python indexing starts at 0, making stringsize - 1 the last index. Because empty is an empty list, you cannot index into it. Using the list.append() method adds new values at the end instead.
You don't really need the stringsize reference there, because negative indices automatically are subtracted from the length for you:
for x in range(len(c)):
empty.append(c[-x-1])
Since this is supposed to return a string, not a list, you need to join the characters again at the end:
return ''.join(empty)
The easiest way to reverse a list is to use a negative slice stride:
def reverse(c):
return c[::-1]
IndexErrorbecauseemptylist you have does not have anything in it, it is basically empty. You shouldappendto it. Check python docs.reversedfunction to reverse your string. something like this:"".join(list(reversed("foo"))), though there may exist even simpler solutions. I'm callinglist()becausereversedreturns a "reversed object", which I then need to turn back into a string.IndexErrorbecausec[stringsize]is out of bounds; the last index isc[stringsize - 1].str.join()calls list on thereversed()iterator for you..