How to replace a set of characters inside another string in Python?
Here is what I'm trying to do: let's say I have a string 'abcdefghijkl' and want to replace the 2-d from the end symbol (k) with A. I'm getting an error:
>>> aa = 'abcdefghijkl'
>>> print aa[-2]
k
>>> aa[-2]='A'
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
aa[-2]='A'
TypeError: 'str' object does not support item assignment
So, the question: is there an elegant way to replace (substitute) with a string symbols inside another string starting from specified position? Something like:
# subst(whole_string,symbols_to_substiture_with,starting_position)
>>> print aa
abcdefghijkl
>>> aa = subst(aa,'A',-2)
>>> print aa
abcdefghijAl
What would be a not-brute-force code for the subst?