CBSE Class 11 : Python Strings 5

How to change or delete a string?

Strings are immutable. This means that elements of a string cannot be changed once it has been assigned. We can simply reassign different strings to the same name.
n=”computer”
print(n)
computer
n[2]=’r’
print(n)
#error: TypeError: ‘str’ object does not support item
assignment
Note: we can assign the string by another value but cannot change any character by using index.
n=”computer”
print(n)
n=’hello’
print(n)
computer
hello

 

Deletion of characters from a string

We cannot delete or remove characters from a string. But deleting the string entirely is possible using the keyword del.
n=”computer”
print(n)
computer
del n[1]
#Error :TypeError: ‘str’ object doesn’t support item deletion
print(n)

But we can delete the entire string
n=”computer”
print(n)
del n
print(n)
Note:
Error :NameError: name ‘n’ is not defined