Your code has a few flaws:
- Why are you iterating over the string if the only thing you need to check is the first and the last character?
- While iterating a string you should not change it. It leads to unexpected and undesired consequences.
- Your Boolean logic seems odd.
- You are replacing all of the quotes in the first loop.
What would work is this:
if string[0] == "'" and string[-1] == "'" and len(string) > 1: string = string[1:-1]
Where you do pretty much the same checks you want but you just remove the quotations instead of alternating the inner part of the string.
You could also use string.strip("'") but it is potentially going to do more than you wish removing any number of quotes and not checking if they are paired, e.g.
"'''three-one quotes'".strip("'") > three-one quotes