小编bbr*_*all的帖子

Python + =运算符是否使字符串可变?

当我尝试使用+ =运算符修改字符串,并使用id()方法来检查对象的标识时,字符串似乎是可变的.有人面对这种奇怪的python行为吗?

a = '123'

print id(a)
# 89806008

a += '1'

print id(a)
# 89245728

a += '1'

print id(a)
# 89245728

print a

# '12311'
Run Code Online (Sandbox Code Playgroud)

使用 a = a + '1'doesnt具有相同的效果,并更改字符串ID.

python string mutability python-2.7

2
推荐指数
1
解决办法
229
查看次数

标签 统计

mutability ×1

python ×1

python-2.7 ×1

string ×1