字符串格式错误

wro*_*ame 1 python string format

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

AttributeError: 'str' object has no attribute 'format'
Run Code Online (Sandbox Code Playgroud)

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

from distutils.command.bdist_dumb import format
Run Code Online (Sandbox Code Playgroud)

这本身就会导致错误

ImportError: cannot import name format
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

all*_*ode 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'
Run Code Online (Sandbox Code Playgroud)

但是,您将在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'
Run Code Online (Sandbox Code Playgroud)

这个功能似乎已被反向移植到Python 2.6,所以你不会在那里得到这个错误.您必须运行Python <2.6.