optimum way of assigning to a string slice

Jonathan P. jbperez808 at yahoo.com
Fri Jan 10 21:20:55 EST 2003


I'm creating a text widget input control in the form of a 
class, called EditField, and it has, as a class member, a string
EditField.content, containing the current input.  Everytime 
the user types in a new character, I have to modify EditField.content 
to reflect the letter the user typed in.

The clearest way I can think of doing this would be something like:

self.content[cursorpos:cursorpos+1]=keypress

Of course, this won't work because of string immutability.
Thus the workaround would be to create a newstring:

self.content=self.content[0:cursorpos]+keypress+self.content[cursorpos+1:]

Is there a better, more efficent way?




More information about the Python-list mailing list