Replace single character at given position
coolazNOSPAM at web.de
Tue Sep 19 23:35:48 CEST 2006
How do I replace a single character in a string at a given position?
>From programming languages like C I expect something like that:
>>> idx = 1
>>> s1 = "pxthon"
>>> s1[idx] = 'y'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object does not support item assignment
It does not work :-(
My second attempt succeeds:
>>> s1 = s1[0:idx] + 'y' + s1[idx+1:]
Is this a typical approach in Python to replace one character
in a string? 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.
Thanks in advance,
More information about the Python-list