Need help with a string plz! (newbie)

Grant Edwards grante at visi.com
Fri Mar 9 22:37:22 CET 2007


On 2007-03-09, israphelr at googlemail.com <israphelr at googlemail.com> wrote:

> #################################
> print "\nWelcome !"
> print "\nEnter a word, and the world will be reversed!"
>
> word = raw_input("\nPlease Enter a word: ")
>
> end = len(word)
> end -= 1
>
> for position in range(end, -1, -1):
>    print word[position],
>
> raw_input("\nPress Enter to Exit")
>
> ################################
>
> What actually happens is, the next character is printed on the same
> line but, there is a space inbetween each character.

Yup, that's what print does.  Try sys.stdout.write(word[position]).
You'll have to import sys, of course.

or try this:

print ''.join(reversed(list(word)))

or this:

print word[-1::-1]

I would have sworn there was a string method that returned the
reversed string, but there doesn't seem to be in 2.4.3...

> I thought maybe I could create another variable and then assign each
> character into the new string by concatenating, makign a new string
> each time, but I find this a bit muddling at the moment. Any help'd be
> great. Thanks.

s = ''
for c in word:
    s = c + s
print s

-- 
Grant Edwards                   grante             Yow!  Here I am at the flea
                                  at               market but nobody is buying
                               visi.com            my urine sample bottles...



More information about the Python-list mailing list