新的字符串格式化方法

时间:2016-11-15 01:22:32

标签: python python-3.x

我正在通过" Python 101"当Mathew Driscoll尝试这个问题时遇到了一个问题。

>>> print("%(lang)s is fun!" % {"lang":"python"})

我理解我试图在这里使用Python字典,而lang是一个"键值对" 我所做的只是复制书中的内容,但是这本书已经出了一段时间,而且我使用的是最新版本的Python。

这是我得到的错误:

 Traceback (most recent call last):
  File "<pyshell#78>", line 1, in <module>
    print("%(lang)s is fun!" % {"lang":"python"})
TypeError: 'str' object is not callable

1 个答案:

答案 0 :(得分:3)

您将变量命名为print。不要这样做(一般情况下,永远不要命名与Python内置同名的变量,因此没有名为strintlist,{{的变量1}},set等)。要修复当前的解释器会话,请运行:

dict

将删除隐藏内置del print 函数的print变量,恢复原始print函数。那条线就可以了。

相关问题