Stepping backwards in for loop?
Carlos Ribeiro
cribeiro at mail.inet.com.br
Sat Apr 14 12:37:54 EDT 2001
At 14:10 14/04/01 +0000, Gustaf Liljegren wrote:
>Can't figure out how to step backwards, character by character in a string.
>I was looking for a loop countruct with a counter to handle this, but the
>for loop in Python doesn't let me step backwards. What is the alternative?
>>> a = 'abcde'
>>> b = list(a)
>>> b.reverse()
>>> b
['e', 'd', 'c', 'b', 'a']
It would be a *lot* easier if strings had a reverse method, or if the
reverse() methods returned the reversed string. However, similarly to
sort(), the Python-way-of-doing-things must have some good reason for
reverse() to behave this way (as a inplace operation on the list).
Carlos Ribeiro
More information about the Python-list
mailing list