Python 實現str類型修改的方法

Python的str是不能修改的,但是我們可以通過切片操作來變相的實現插入 刪除和修改等操作。

lonfee@ubuntu:~$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a='0034'
>>> a[1]='1'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> a=a[:1]+'1'+a[2:]
>>> print a
0134
>>> a=a[:2]+'2'+a[2:]
>>> print a
01234
>>> 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章