如何在 Python 中解决此错误:“TypeError: ‘str’ object is not callable”

时间:2021-02-16 13:05:11

标签: python

我该如何解决这个错误?

我在尝试运行的每个示例中都面临此错误。我是 Python 新手,并试图在我的代码中添加空格。

这是我的代码

spaces = '' * 12
print('add some spaces in this text' % spaces)

我收到此错误

TypeError: 'str' object is not callable

3 个答案:

答案 0 :(得分:3)

应该是

spaces = ' ' * 12
print('Add some spaces in this text:' + spaces + 'That was some spaces!')

答案 1 :(得分:2)

我会在这里使用 f 字符串文字:

spaces = ' ' * 12
print(f'Add some spaces in this text: {spaces} That was some spaces!')

答案 2 :(得分:0)

修复

spaces= ' ' * 12
print('add some spaces in this text'.replace(' ', spaces))

输出

add            some            spaces            in            this            text