字符串格式错误

时间:2010-03-15 20:19:57

标签: python string format

使用Python 3.1.1中的代码print('{0} is not'.format('That that is not')),我收到以下错误:

AttributeError: 'str' object has no attribute 'format'

当我删除开头自动插入的Netbeans行时:

from distutils.command.bdist_dumb import format

本身会导致错误

ImportError: cannot import name format

我在这里做错了什么?

1 个答案:

答案 0 :(得分:6)

您必须运行旧版本的Python。这在Python 3.1.1 +中起作用:

$ python3
Python 3.1.1+ (r311:74480, Nov  2 2009, 14:49:22) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '{0} is not'.format('That that is not')
'That that is not is not'

但是,您将在Python 2.5.4中收到此错误:

$ python2.5
Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '{0} is not'.format('That that is not')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'format'

此功能似乎已经向后移植到Python 2.6,因此您不会在那里收到此错误。你必须运行Python&lt; 2.6。