-1

TypeError: 'str' object does not support item assignment

I am taking a 11 digit number as input in recieved_code, later on after getting the error position, I want to replace that digit with 0 or 1 depending on the condition.

recieved_code = (input())

#Detecting the position of error

parList = parList[: : -1]
error = 0
for i in range(0,len(parList)):
    error = error + pow(2,i)*parList[i]
print("\nError is at position:",error)

#Correcting the error
if recieved_code[error-1] == 1:
    print("It's 1")
    recieved_code[error-1] = 0
else:
    print("It's 0")
    recieved_code[error-1] = 1

print("Corrected code:",recieved_code)

I wanted 0 to be replaced by 1, or 1 to be replaced by 0.

This is the error

Duck Dodgers
  • 3,409
  • 8
  • 29
  • 43
  • 9
    Python Strings are immutable. 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) – Austin Feb 07 '19 at 17:01

1 Answers1

1

Strings are immutable

That means that once created, you can't modify the string itself. You need to create another one after processing/concatenating/etc...

Community
  • 1
  • 1
molamk
  • 4,076
  • 1
  • 13
  • 22