Python encountered an error saying 'str' object does not support item deletion.

Asked 2 years ago, Updated 2 years ago, 64 views

Hello, I am studying Python

I wanted to delete a specific index in the array using del, so I made the following code.

'str' object does not support item deletion.

There was no error before writing this sentence.

What's the problem? If you know it, I'd appreciate it if you could answer it ㅠ<

python error

2022-09-22 18:49

1 Answers

Python cannot perform del operations on elements of a particular index of a string.

In similar cases, tuple or del operations cannot be performed for elements in a particular index.

del changes the target that holds the element by removing it. list is the type that can insert and remove elements, whereas str and tuple are the types that cannot insert and remove elements after being generated once.

The type that is created once and cannot be changed afterwards is called imutable type.

So how do we subtract only certain elements from file_data? Because the str object is impossible, you must eventually create a new string object and substitute it in the file_data variable. The question you asked seems to be trying to erase the [019) range, so you can cut the 19 index to the end and put it in file_data as shown below.

file_data = file_data[19:]


2022-09-22 18:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.