相关疑难解决方法(0)

Python版本早于2.6的字符串格式

当我在Python 2.5.2中运行以下代码时:

for x in range(1, 11):
    print '{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x)
Run Code Online (Sandbox Code Playgroud)

我明白了:

Traceback (most recent call last):
  File "<pyshell#9>", line 2, in <module>
    print '{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x)
AttributeError: 'str' object has no attribute 'format'
Run Code Online (Sandbox Code Playgroud)

我不明白这个问题.

dir('hello')没有format属性.

我怎么解决这个问题?

python format

34
推荐指数
5
解决办法
5万
查看次数

标签 统计

format ×1

python ×1