TypeError: object 'str' does not support object mapping (Python)

advertisements

So this is what I'm trying to do:

input = ABCDEFG

***DEFG
A***EFG
AB***FG
ABC***G
ABCD***

and this is the code I wrote

def loop(input):
    output = input
    for index in range(0, len(input)-3):              #column length
        output[index:index +2] = '***'
        output[:index] = input[:index]
        output[index+4:] = input[index+4:]
        print output + '\n'

But I get the error: TypeError: 'str' object does not support item assignment

Help?


You cannot modify the contents of a string, you can only create a new string with the changes. So instead of the function above you'd want something like this

def loop(input):
    for index in range(0, len(input)-3):              #column length
        output = input[:index] + '***' + input[index+4:]
        print output