本文介紹了早于 2.6 的 Python 版本中的字符串格式的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
當(dāng)我在 Python 2.5.2 中運(yùn)行以下代碼時(shí):
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)
我明白了:
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'
我不明白這個(gè)問(wèn)題.
從 dir('hello')
沒(méi)有 format
屬性.
我該如何解決這個(gè)問(wèn)題?
How can I solve this?
推薦答案
str.format
方法是 在 Python 3.0 中引入,并向后移植到 Python 2.6 及更高版本.
The str.format
method was introduced in Python 3.0, and backported to Python 2.6 and later.
這篇關(guān)于早于 2.6 的 Python 版本中的字符串格式的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!