"Gustaf Liljegren" <gustafl at algonet.se> wrote in message news:Xns9083A46CB3B93gustaflalgonetse at 194.213.69.152... > 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? You have several alternatives, such as: thestr = 'ciao' for x in range(len(thestr), 0, -1): print thestr[x-1], print for x in range(len(thestr)): print thestr[-x-1], print aulist = list(thestr) aulist.reverse() for c in aulist: print c, print class Reverse: def __init__(self, seq): self.seq = seq def __getitem__(self, index): return self.seq[-index-1] for c in Reverse('ciao'): print c, print Each of these snippets will print 'o a i c'. Alex
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4