I'm trying to figure out how to reverse a string using Python without using the [::-1] solution.
My code seems to work fine for several test cases, but it adds an extra space for one instance and I can't figure out why.
def reverse(s):
r = list(s)
start, end = 0, len(s) - 1
x = end//2
for i in range(x):
r[start], r[end] = r[end], r[start]
start += 1
end -= 1
print(''.join(r))
reverse('A man, a plan, a canal: Panama')
# returns 'amanaP :lanac a,nalp a ,nam A'
# note the double space ^^ - don't know why
reverse('a monkey named fred, had a banana')
# 'returns ananab a dah ,derf deman yeknom a'
reverse('Able was I ere I saw Elba')
# returns 'ablE was I ere I saw elbA'