[Tutor] Tutor Digest, Vol 40, Issue 25
vishnu at montalvosystems.com
Tue Jun 12 13:01:49 CEST 2007
> Message: 2
> Date: Sun, 10 Jun 2007 09:20:28 -0500
> From: David Hamilton <dsh0105 at comcast.net>
> Subject: [Tutor] Correct use of range function..
> To: tutor at python.org
> Message-ID: <466C08AC.2030201 at comcast.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> I just finished doing an exercise in a tutorial on the range function
> and while I got it to work, my answer seems ugly. I'm wondering if I'm
> missing something in the way I'm using the range function.
> The tutorial ask me to print a string backwards. My solution works, but
> it it just doesn't "feel" right :). My result is difficult to read and
> I feel like I'm probably over complicating the solution. Suggestions?
> #Start at the end of the string, count back to the start, printing each
> for i in range(len(word)-1,-1,-1):
> print word[i],
Another best way of doing it is:
word = 'reverse'
rev = ''
for i in range(len(word)):
rev += word[-(i+1)]
More information about the Tutor