[Tutor] Re: Changing a list item in place

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 11 Feb 2002 10:21:07 -0800 (PST)


On Mon, 11 Feb 2002, Chris McCormick wrote:

> 	You are absolutely right.  I was trying to change the second
> character of a string contained at map[y_box][x_box].  Along with
> renaming my map, I'll find another way to do it...

By the way, if we want to be able to fiddle around with the individual
characters of strings, we'll find it easier to do if we convert the string
to a list:

###
>>> greeting = 'hallo'
>>> greeting_list = list(greeting)
###


Once we have a list representation of our string, we can fiddle around
with it, like this:

###
>>> greeting_list[1] = 'e'
>>> greeting = str(greeting_list)
>>> greeting
"['h', 'e', 'l', 'l', 'o']"                        ## Yikes!
>>> greeting = string.join(greeting_list, '')
>>> greeting
'hello'                                            ## That's better.
###


Always useful to know if we plan to do small string surgery soon.  *grin*