>>> s = 'Python is the best programming language' >>> s[::-1] 'egaugnal gnimmargorp tseb eht si nohtyP'
UPD:
if you need to do this in a loop, you can use a range to go backward:
>>> result = "" >>> for i in xrange(len(s)-1, -1, -1): ... result += s[i] ... >>> result 'egaugnal gnimmargorp tseb eht si nohtyP'
or, reversed()
:
>>> result = "" >>> for i in reversed(s): ... result += i ... >>> result 'egaugnal gnimmargorp tseb eht si nohtyP'
source share