How to Reassign and Delete the String in python?

Reassigning Strings
Updating the content of the strings is as easy as assigning it to a new string. The string object doesn’t support item assignment i.e., A string can only be replaced with new string since its content cannot be partially replaced. Strings are immutable in Python.

Consider the following example.

Example 1
str = “HELLO”
str[0] = “h”
print(str)
Output:

Traceback (most recent call last):
  File "12.py", line 2, in <module>
    str[0] = "h";
TypeError: 'str' object does not support item assignment

However, in example 1, the string str can be assigned completely to a new content as specified in the following example.

Example 2
str = “HELLO”
print(str)
str = “hello”
print(str)
Output:

HELLO
hello  

Deleting the String

As we know that strings are immutable. We cannot delete or remove the characters from the string. But we can delete the entire string using the del keyword.

str = "JAVATPOINT"  
del str[1]  

Output:

TypeError: 'str' object doesn't support item deletion
Now we are deleting entire string.

str1 = "JAVATPOINT"  
del str1  
print(str1)  

Output:

NameError: name 'str1' is not defined