replace value of a specific position

Cliff Wells LogiplexSoftware at earthlink.net
Thu Jul 10 14:01:21 EDT 2003


On Thu, 2003-07-10 at 10:36, Tom wrote:
> Hi,
> 
> I want to replace a specific part of a string. I tried replace but this 
> doesn't work the way I want it, because it checks and replaces all 
> values and not the position (index).
> 
> t = '010010101001001110100101010111'
> t = t.replace(t[5], '2')
> 
> This is just a bad example of what I tried. It produces this output:
> 
> 212212121221221112122121212111
> 
> But I wanted:
> 
> 010012101001001110100101010111

>>> def strrep(s, i, c):
...     return s[:i] + c + s[i + 1:]
...
>>> t = '010010101001001110100101010111'
>>> strrep(t, 5, '2')
'010012101001001110100101010111'

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726  (800) 735-0555






More information about the Python-list mailing list