[Tutor] Working with strings

Kalle Svensson kalle@gnupung.net
Mon, 12 Mar 2001 17:29:05 +0100


Sez Pedro Diaz Jimenez:
> >>> def backword(s):
> ...     index = len(s)-1
> ...     while index >= 0:
> ...             print s[index],
> ...             index = index-1
> ...
> >>> backword("hola")
> a l o h
> >>>
> 
[would like a recursive version]

def backword(s):
    if s:
        print s[-1],
        backword(s[:-1])

I might have written the iterative version like this:

def backword(s):
    while s:
        print s[-1],
	s = s[:-1]

Peace,
  Kalle
-- 
Email: kalle@gnupung.net     | You can tune a filesystem, but you
Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD
 [ Not signed due to lossage.  Blame Microsoft Outlook Express. ]