Need help with a string plz! (newbie)

Larry Bates lbates at websafe.com
Fri Mar 9 16:58:41 EST 2007


israphelr at googlemail.com wrote:
> On Mar 9, 9:37 pm, Grant Edwards <gra... at visi.com> wrote:
>> On 2007-03-09, israph... at googlemail.com <israph... at googlemail.com> 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



More information about the Python-list mailing list