在python中,数据类型分为可变数据类型和不可变数据类型,不可变数据类型包括string,int,float,tuple,可变数据类型包括list,dict。
所谓的可变与不可变,举例如下:
>>> a = "test" >>> print a[0] t >>> a[0] = 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment >>> a = [1, 2, 34] >>> print a[0] 1 >>> a[0] = 4 >>> a [4, 2, 34] >>>
>>> a = "hello world"
>>> a.replace("world", "python")
'hello python'
>>> a
'hello world'
>>> a = a.replace("world", "python")
>>> a
'hello python'
>>>