[Tutor] Python backwards program (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Apr 15 20:14:13 CEST 2005


[Jim, when you reply, please send to tutor at python.org.  Don't just send
your reply only to me.  Use your email client's Reply-To-All feature.]



[Jim]
>   > The following gives me the last letter of the string.
>   >
>   > backwords=raw_input("enter number or string:")
>   > print backwords[-1]


[Danny]
>   Yes, this is an interesting approach!  We can go along this approach a
>   little more, if you'd like.  The code above is getting the last letter, so
>   we're closer to printing out the word backwards.
>
>   How would you print out the second to last letter of the string?


[Jim]
> To print out the second to the last letter it would be
> backwords[-2]



Yes.  So let's see what we have so far:

######
backwords=raw_input("enter number or string:")
print backwords[-1]
print backwords[-2]
######

This works if the 'backwords' word is two letters long.

So it's a partial solution: we still need to do a little bit to make this
work on any word.



More information about the Tutor mailing list