【问题标题】:'module' object has no attribute 'format'“模块”对象没有属性“格式”
【发布时间】:2013-11-23 17:29:50
【问题描述】:

我尝试用更精确的数字显示数字。

import string    
print string.format(" %.5f", 0.5)

但是这个给我一个错误:

AttributeError: 'module' 对象没有属性 'format'

【问题讨论】:

    标签: python string python-2.7


    【解决方案1】:

    使用% 运算符或str.format

    >>> "%.5f" % 0.5
    '0.50000'
    >>> "{:.5f}".format(0.5)
    '0.50000'
    >>>
    

    【讨论】:

    • 谢谢假货!操作比我的清楚多了。但是string.format的使用有问题吗?
    • @user3023475,format 模块中没有 format 函数。
    猜你喜欢
    • 1970-01-01
    • 2018-03-11
    • 2017-03-29
    • 2013-02-13
    • 2010-11-18
    • 2019-01-03
    • 2017-05-19
    • 2013-02-01
    相关资源
    最近更新 更多