【发布时间】:2017-02-10 14:39:30
【问题描述】:
假设我们有一些这样的代码:
placehold = "6"
string1 = "this is string one and %s" % placehold
print string1
placehold = "7"
print string1
运行时,两个 print 语句都返回好像 placehold 始终为 6。但是,就在第二个语句运行之前,placehold 更改为 7,那么为什么它没有动态反映在字符串中?
另外,你能建议一种方法让字符串返回 7 吗?
谢谢
【问题讨论】:
-
字符串是不可变的。他们不会改变。使用
StringVar之类的东西或重新分配 -
您需要在更改为
placehold的值后再次执行string1 = "this is string one and %s" % placehold以使string1的值发生更改。
标签: python string python-2.7 string-formatting