'str' index out of range on codingbat.com

When i’m on codingbat.com, i saw this question that asks: Given a string, return a new string where the first and last chars have been exchanged and i answered:

def front_back(str):
  string = str[len(str)-1]+str[1:len(str)-1]+str[0]
  return(string)

And codingbat told me string index out of range. I tested it on python 3.8.2 on my computer and it works fine. I’m a noob at programming. Help plz.

Can you provide the exact problem?

You might have an index error when the string is empty (""). What do the instruction say of such corner cases?

It didn’t say anything about such corner cases. And the page just said that:
Compile problems:

string index out of range

see Python Example Code to help with compile problems

Can’t you give me where you execute the code? So i can reproduce the error.

you could try to catch the error:

try:
   string = str[len(str)-1]+str[1:len(str)-1]+str[0]
except IndexError:
   print(str)

so you can see what str is, and work out why you get an index error.