String is Immutable.

Strings are Immutable:

# Mutable: Can change # Immutable: Can not change

The String in python are immutable. You can not change an existing character of a string. Let us consider :


indexing

st="hello" print(st[0]) output: h st[0]="t" output: st type error

Type error st object does not support item assignment. The object in this case is the string st and the item is character you try to assign , an object is the same thing as a value and item is one of the value of a sequence. To achieve changes like modification in a String, we can create new String.

t="hello" t1='t'+t[1:] print(t1) output: tello Consider the following two similar String "Hello" is assign to two different variables as: str="hello" str2="hello"

In the above example str1 and str2 have the same contents, Python uses one object for each string which has same content.

>>>id (str1) 139970627545328 >>>id (str2) 139970627545328

Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.