'in-place' string modifications in Python(stackoverflow.com) |
'in-place' string modifications in Python(stackoverflow.com) |
Eg. for a single replacement (I haven't seen this one mentioned), slicing might work best: s[:i-1] + F(s[i]) + s[i+1:]. This is obviously good for a small number of replacements too, though you might want to switch to "".join(iterable) for better performance.
In general, immutability of strings requires a more functional approach than in-place editing.