Borislav Hadzhiev
Last updated: Apr 20, 2022
Check out my new book
The Python "TypeError: 'str' object does not support item assignment" occurs when we try to modify a character in a string. Strings are immutable in Python, so we have to convert the string to a list, replace the list item and join the list elements into a string.
Here is an example of how the error occurs.
my_str = 'abc' # ⛔️ TypeError: 'str' object does not support item assignment my_str[0] = 'z'
We tried to change a specific character of a string which caused the error.
One way to replace a character at a specific index in a string is to:
my_str = 'abc' my_list = list(my_str) print(my_list) # 👉️ ['a', 'b', 'c'] my_list[0] = 'z' new_str = ''.join(my_list) print(new_str) # 👉️ 'zbc'
We passed the string to the list()
class to get a list where each character of
the string is a list item.
The last step is to join the list items into a string with an empty string separator.
The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
An alternative approach is to use string slicing.
Here is an example that replaces an underscore at a specific index with a space.
my_str = 'hello_world' idx = my_str.index('_') print(idx) # 👉️ 5 my_new_str = my_str[0:idx] + ' ' + my_str[idx+1:] print(my_new_str) # 👉️ "hello world"
The first piece of the string we need is up to, but not including the character we want to replace.
So, my_str[0:idx]
is a slice of the string that starts at index 0
and goes
up to, but not including idx
.
my_str = 'hello_world' print(my_str[0:5]) # 👉️ "hello"
The next step is to use the addition +
operator to add the replacement string
(in our case - a space).
The last step is to concatenate the rest of the string. Notice that we start the
slice at idx + 1
because we want to omit the character we are replacing.
my_str = 'hello_world' print(my_str[5 + 1:]) # 👉️ "world"
We don't specify an end index after the colon. This means - go til the end of the string.
We simply construct a new string excluding the character at the specific index and providing a replacement string.
my_str = 'hello_world' idx = my_str.index('_') print(idx) # 👉️ 5 my_new_str = my_str[0:idx] + ' ' + my_str[idx+1:] print(my_new_str) # "hello world"
Another alternative is to use the replace()
method.
my_str = 'apple, banana, apple' my_new_str = my_str.replace('apple', 'melon') print(my_new_str) # 👉️ "melon, banana, melon"
The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.
The method takes the following parameters:
Name | Description |
---|---|
old | The substring we want to replace in the string |
new | The replacement for each occurrence of old |
count | Only the first count occurrences are replaced (optional) |
The difference with the previous two approaches is that the replace()
method
takes the substring we want to replace as a parameter (not an index).
The method also replaces all occurrences of the substring.