M
Martin Kulas
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:
File "<stdin>", line 1, in ?
TypeError: object does not support item assignment
It does not work :-(
My second attempt succeeds:
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
How do I replace a single character in a string at a given position?
From programming languages like C I expect something like that:
Traceback (most recent call last):idx = 1
s1 = "pxthon"
s1[idx] = 'y'
File "<stdin>", line 1, in ?
TypeError: object does not support item assignment
It does not work :-(
My second attempt succeeds:
'python's1 = s1[0:idx] + 'y' + s1[idx+1:]
s1
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