Foundations of Python

91 / 134

Immutability of Strings

Immutable means once a string is created, it can't be edited or modified.

Then, guess what does this print?

a = "cloudx"
a = a + "lab"

It prints cloudxlab as per your guess. But, the original string isn't modified here.

a is just a pointer to the str object in the memory which gets assigned a new value when the new string is created. Remember, here a new string is created by concatenating copy of a and "lab" and then assigned to a again. It has no effect on the original string i.e. "cloudx"

(We will study objects later. For now, an object is the same thing as a value)

So, we can't change a character inside the string by using the indexing,

a = "cloudxlab"
a[1] = 'm'

Understand and write this code in the notebook and observe you receive,

a = "cloudxlab"
a[1] = 'm'

No hints are availble for this assesment

Answer is not availble for this assesment

Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...