Need help with a string plz! (newbie)

2007-03-09 Thread israphelr
Hi all

So I created a program, that gets a string from a user and then prints
the string in reverse order.

Now, here's the code;

#
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.

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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a string plz! (newbie)

2007-03-09 Thread israphelr
On Mar 9, 9:29 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> In <[EMAIL PROTECTED]>, israphelr
> wrote:

> In [57]: 'reversed string'[::-1]
> Out[57]: 'gnirts desrever'


Sorry, I didn't understand this.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a string plz! (newbie)

2007-03-09 Thread israphelr
On Mar 9, 9:37 pm, Grant Edwards <[EMAIL PROTECTED]> wrote:
> On 2007-03-09, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> > 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
>


Okay thanks very much, I used this method, since it's cloest one to I
am already faimilar with.

Here's the final code..(if anyones interested)

##

print "\nWelcome !"
print "\nEnter a word, and the world will be reversed!"

word = raw_input("\nPlease Enter a word: ")

end = len(word)
end -= 1
new_string = ""

for position in range(end, -1, -1):
   new_string += word[position]

print new_string

raw_input("\nPress Enter to Exit")

###

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a string plz! (newbie)

2007-03-09 Thread israphelr
On 9 Mar, 21:58, Larry Bates <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > On Mar 9, 9:37 pm, Grant Edwards <[EMAIL PROTECTED]> wrote:
> >> On 2007-03-09, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> >>> 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
>
> > Okay thanks very much, I used this method, since it's cloest one to I
> > am already faimilar with.
>
> > Here's the final code..(if anyones interested)
>
> > ##
>
> > print "\nWelcome !"
> > print "\nEnter a word, and the world will be reversed!"
>
> > word = raw_input("\nPlease Enter a word: ")
>
> > end = len(word)
> > end -= 1
> > new_string = ""
>
> > for position in range(end, -1, -1):
> >new_string += word[position]
>
> > print new_string
>
> > raw_input("\nPress Enter to Exit")
>
> > ###
>
> If you are really trying to learn Python, you really owe it to
> yourself to study Grant's replies.  You are using constructs
> from some previous language that you learned and trying to
> apply them to Python.  IMHO Python programmers would never use
> s=c+s on strings as it is quite inefficient.  The methods
> and functions he uses reversed(), .join(), list() are very
> powerful Python constructs that you will need to learn how
> to use.  The slicing word[-1::-1] is also something that
> will come in handy later if you learn how to use it.  This
> is a simple example that you can learn a lot from if you
> will study Grant's responses.
>
> Just my two cents.
>
> -Larry

Oh, thanks for the advice then. And as for Grant..look forward to
seeing more of your posts.

-- 
http://mail.python.org/mailman/listinfo/python-list