What is the best way to loop over a python string backwards?
The following seems a little awkward for all the need of -1 offset:
string = \"trick or
Yes, the second syntax shortcut creates an intermediate string and has an associated performance penalty.
The first version is better written as:
for index, char in enumerate(reversed(s)):
print "pos %d: %s" % (index, char)
Which is easy to comprehend. Neither reversed nor enumerate` need to make a copy of the string.
Also be careful about using string as a variable name, as it is also the name of a module in the standard library.