TypeError: 'str' object does not support item assignment

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

TypeError: 'str' object does not support item assignment #

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.

typeerror str object does not support item assignment

Here is an example of how the error occurs.

main.py
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.

Strings are immutable, so in order to get the result we need, we have to create a new, updated string.

One way to replace a character at a specific index in a string is to:

  1. Convert the string to a list.
  2. Update the list item at the specific index.
  3. Join the list items into a string.
main.py
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.

Lists are mutable, so we are able to change the list item at the specific index.

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.

main.py
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.

main.py
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.

main.py
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.

main.py
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.

main.py
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:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)
Note that the method doesn't change the original string. Strings are immutable in Python.

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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.