4

So this is what I'm trying to do:

input: ABCDEFG

Desired output:

***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

3
  • 4
    Strings are immutable. If you want to change it, transform it into a list, manipulate the list, then jon it back to a string. Commented Sep 16, 2015 at 19:26
  • how would i do that? Commented Sep 16, 2015 at 19:30
  • Not that this is your issue, but input is a builtin function in Python, best not to overwrite it with a variable. Commented Feb 11, 2022 at 6:26

5 Answers 5

2

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
Sign up to request clarification or add additional context in comments.

Comments

2

Strings are immutable. You can not change the characters in a string, but have to create a new string. If you want to use item assignment, you can transform it into a list, manipulate the list, then join it back to a string.

def loop(s):
    for index in range(0, len(s) - 2):
        output = list(s)                    # create list from string
        output[index:index+3] = list('***') # replace sublist
        print(''.join(output))              # join list to string and print

Or, just create a new string from slices of the old string combined with '***':

        output = s[:index] + "***" + s[index+3:] # create new string directly
        print(output)                            # print string

Also note that there seemed to be a few off-by-one errors in your code, and you should not use input as a variable name, as it shadows the builtin function of the same name.

Comments

1

In Python, strings are immutable - once they're created they can't be changed. That means that unlike a list you cannot assign to an index to change the string.

string = "Hello World"
string[0] # => "H" - getting is OK
string[0] = "J" # !!! ERROR !!! Can't assign to the string

In your case, I would make output a list: output = list(input) and then turn it back into a string when you're finished: return "".join(output)

Comments

1

In python you can't assign values to specific indexes in a string array, you instead will probably want to you concatenation. Something like:

for index in range(0, len(input)-3):
    output = input[:index]
    output += "***"
    output += input[index+4:]

You're going to want to watch the bounds though. Right now at the end of the loop index+4 will be too large and cause an error.

1 Comment

output = input[:input] should be output = input[:index]
0

strings are immutable so don't support assignment like a list, you could use str.join concatenating slices of your string together creating a new string each iteration:

def loop(inp):
    return "\n".join([inp[:i]+"***"+inp[i+3:] for i in range(len(inp)-2)])

inp[:i] will get the first slice which for the first iteration will be an empty string then moving another character across your string each iteration, the inp[i+3:] will get a slice starting from the current index i plus three indexes over also moving across the string one char at a time, you then just need to concat both slices to your *** string.

In [3]: print(loop("ABCDEFG"))
***DEFG
A***EFG
AB***FG
ABC***G
ABCD***

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.