Modify one character in a string

keirr keir.robinson at gmail.com
Thu May 25 06:21:28 EDT 2006


mp wrote:
> X-No-Archive
> How do I go about modifying one character in a string elegantly?
> In other words, I want a function that will change 'aaaa' to 'aaza',
> given the index 2 of the character in the string.
>
> Also, how do I do this when dealing with a file ; which file mode
> should I use and what function should I use to modify a single
> character once in that file mode?
>
> Thanks
> MP

Technically you can't - strings are immutable.  If you use the StringIO
module then you can
use a common approach between 'strings' in memory, and files, using
seek and write.

All the best,

 Keir.




More information about the Python-list mailing list