Python版本早于2.6的字符串格式 [英] String formatting in Python version earlier than 2.6
本文介绍了Python版本早于2.6的字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python 2.5.2中运行下面的代码:
for x in range(1,11):
print'{0:2d} {1:3d} {2:4d}'。
$ b
$ b
Traceback(最近的最后一次调用):
在< module>中,第2行的文件< pyshell#9>
print'{0:2d} {1:3d} {2:4d}'格式(x,x * x,x * x * x)
AttributeError:'str'对象没有属性'format'
我不明白这个问题。
从 dir('hello')
没有格式
属性。
如何解决这个问题?
解决方案 str.format
方法是介绍Python 3.0,并将它们移植到Python 2.6及更高版本。
When I run the following code in Python 2.5.2:
for x in range(1, 11):
print '{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x)
I get:
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'
I don't understand the problem.
From dir('hello')
there is no format
attribute.
How can I solve this?
解决方案 The str.format
method was introduced in Python 3.0, and backported to Python 2.6 and later.
这篇关于Python版本早于2.6的字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文