[Tutor] Fwd: Strings backwards

Adam adam.jtm30 at gmail.com
Thu Jan 19 00:25:31 CET 2006


On 18/01/06, ryan luna <ryan_gm at sbcglobal.net> wrote:
>
> Hello, what i need to do is get user input and then
> print the string backwards ^^ i have no idea how to do
> that,
>
> print "Enter a word and i well tell you how to say it
> backwards"
>
> word = raw_input("Your word: ")
>
> print word
>
> all that is simple enough im sure printing it out
> backwards is to, just dont know how ^^, thanks for any help.


How's this for you?

>>> l = list('string')
>>> l
['s', 't', 'r', 'i', 'n', 'g']
>>> l.reverse()
>>> l
['g', 'n', 'i', 'r', 't', 's']
>>> ''.join(l)
'gnirts'

As you can see I converted the string to a list using the list() function
and then applied the reverse method of the list to put it the other way
round and finally used the string method join() to change it back to a
string.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060118/3d35cc01/attachment.htm 


More information about the Tutor mailing list