[Tutor] Program for outputing the letter backward

John Fouhy john at fouhy.net
Wed Mar 29 02:22:36 CEST 2006


On 29/03/06, Hoffmann <oasf2004 at yahoo.com> wrote:
> vehicle='car'
> index = vehicle[-1]       #the last letter
> index_zero = vehicle[0]   #the first letter
>
> while index >= index_zero:
>    letter=vehicle[index]
>    print letter
>    index -= 1
>
> The problem is that I get no output here. Could I hear
> from you?

I can print the letters backwards like this:

vehicle = 'car'
print vehicle[2]
print vehicle[1]
print vehicle[0]

Output:

r
a
c

-----

This is not very useful, though, because it will only work for strings
that are exactly three letters long.  Can you see how to write a loop
to produe this output?

Hint: the len() function will tell you how long a string is.

eg: if vehicle == 'car' then len(vehicle) == 3.

--
John.


More information about the Tutor mailing list