[Tutor] Strings backwards

Adam adam.jtm30 at gmail.com
Thu Jan 19 00:54:37 CET 2006


On 18/01/06, ryan luna <ryan_gm at sbcglobal.net> wrote:
>
>
>
> --- Adam <adam.jtm30 at gmail.com> wrote:
>
> > 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.
> >
>
> Great! thanks, but the last part, for some reason, the
> last part ''.join() isn't working it stays in list
> forms, donno what im doin wrong, heres my code.
>
> print "Enter a word and i well tell you how to say it
> backwards"
>
> word = raw_input("Your word: ")
> word = list(word)
> word.reverse()
> ''.join(word)
>
>
>
> print word
>
> Oh sorry it probably wasn't clear you need to either directly print
''.join(word) or assign it to a variable which could be something like this:
word = ''.join(word) if you want to use the same variable.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060118/908f14fd/attachment.htm 


More information about the Tutor mailing list