现在的位置: 首页 > 综合 > 正文

Python 实现str类型修改的方法

2013年09月06日 ⁄ 综合 ⁄ 共 392字 ⁄ 字号 评论关闭

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
>>> 

抱歉!评论已关闭.