This won't work

I tried using this and it wont work.

Input:
a[5] = "e"
Output:
error!

Expected output that I want is this.
Output:
a = “I die”

It would be helpful if you give some context to your problem. What is a? What you expect a[5] to be? What error occurred?

For example, a="I did"

"e"

image

1 Like

Strings in Python are immutable and you can’t change them, you need to create new object.

1 Like

Your reply crossed over with my earlier questions.

So you have this:

a = 'I did'

and you want the result:

a = 'I dide'

You can do this:

>>> a = 'I did'
>>> a += 'e'  # warning this is slow
>>> a
'I dide'

Doing that with one letter is fast enough, but if you have dozens of
letters to add, it would be better to do it this way:

>>> a = 'I did'
>>> L = list(a)
>>> L.append('e')
>>> L.append(' ')
>>> L.append('z')
>>> a = ''.join(L)
>>> a
'I dide z'

Instead, if you want ‘I died’, you can do it like this:

>>> a = 'I did'
>>> a = a[0:4] + 'e' + a[4:]  # this is called slicing
>>> a
'I died'