Replace single character at given position

Roberto Bonvallet Roberto.Bonvallet at cern.ch
Wed Sep 20 10:12:40 EDT 2006


bearophileHUGS at lycos.com wrote:
> Martin Kulas:
>> Are there better (faster) ways to achieve my goal?
>> I have looked through the methods of type ``string''
>> but I have not found any appropriate method or function.
[...]
> A second way is to use a list of strings with len=1. Like this:
>>>> idx = 1
>>>> s1 = "pxthon"
>>>> l1 = list(s1)
>>>> l1[idx] = 'y'
> ... more processing on the elements of l1, then at the end:
>>>> "".join(l1)
> 'python'

There is a MutableString class that would be a better approach:
http://docs.python.org/lib/module-UserString.html

Anyway, I bet that what Martin wants to do can be done by using only string
methods :)
-- 
Roberto Bonvallet



More information about the Python-list mailing list