我試圖用更精確的數字顯示數字。'模塊'對象沒有屬性'格式'
import string
print string.format(" %.5f", 0.5)
但是這一次把我的錯誤:AttributeError的:「模塊」對象有沒有屬性「格式」 有什麼錯嗎?
我試圖用更精確的數字顯示數字。'模塊'對象沒有屬性'格式'
import string
print string.format(" %.5f", 0.5)
但是這一次把我的錯誤:AttributeError的:「模塊」對象有沒有屬性「格式」 有什麼錯嗎?
使用%
運營商或str.format
:
>>> "%.5f" % 0.5
'0.50000'
>>> "{:.5f}".format(0.5)
'0.50000'
>>>
falsetru的答案是正確的。 string.format
看起來像一個Java語法。 %
在速度上的性能比str.format
好得多,而後者在格式化方面提供了更專業的打字靈活性。
謝謝lennon310。我只是看到人們在某處使用string.format,但不記得:) – user3023475
謝謝falsetru!操作比我的清晰得多。但是string.format的用法有問題嗎? – user3023475
@ user3023475,'string'模塊中沒有'format'函數。 – falsetru