TypeError: 'str' object is not callable

来源:互联网 发布:初中英语语法大全软件 编辑:程序博客网 时间:2024/06/06 02:04
1 出现这个错误一种情况是自己定义了一个str变量覆盖掉了python的str()方法。
for i in range(100):    str = str(i)    print(str)
但我们只是定义str变量,没有覆盖掉str()方法则是可以运行的,但不建议使用python的预留字段作为变量名。
for i in range(100):    str = i    print(str)


2 出现这个错误的另一种情况是字符串在格式化时丢掉%。

print('I %s python'  ('love'))
会出现TypeError: 'str' object is not callable
print('I %s python' % ('love'))  # I love python
加入%号后,运行OK!