0

I am trying to write some code that edit's only a certain part of a string.

I have tried doing something along the lines of number[0][0] = 4 but I always get the error "TypeError: 'str' object does not support item assignment" and I am quite complexed and can not find any information on the web on how to do something along these lines.

List = ['8J', '9D']

List[0][0] = 7

I expect the List to change to ['7J'. '9D'] but all I get is the error "TypeError: 'str' object does not support item assignment".

  • Possible duplicate of ['str' object does not support item assignment in Python](https://stackoverflow.com/questions/10631473/str-object-does-not-support-item-assignment-in-python) – FObersteiner Aug 01 '19 at 10:01
  • see my duplicate link. the error you are getting is because strings are immutable in Python. E.g. lists on the other hand are, so what you could do is change the reference in `List[0]` to e.g. `'7J'` or use the `string` method `.replace()`: `List[0] = List[0].replace('8', '7')`. – FObersteiner Aug 01 '19 at 10:03

1 Answers1

0

Use this:

 List = ['8J', '9D']
 List[0] = List[0].replace(List[0][0],'7')