Hello!

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:]
>>> s1
'python'


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,
Martin



-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to