Modify one character in a string
larry.bates at websafe.com
Thu May 25 19:05:09 CEST 2006
> 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?
IMHO the most elegant method is something like:
def switchchar(srcstring, position, character):
You should open file in binary 'b' mode. Use .seek() method
to seek to the character you want to replace. use .write() method
to write one character.
More information about the Python-list