【问题标题】:In Python's string.format what is the equivalent to %r?在 Python 的 string.format 中,%r 等价于什么?
【发布时间】:2020-02-25 05:33:07
【问题描述】:

我习惯于在string formatting using the % operator 中使用%r,但是如何使用format() 方法获得相同的结果?

>>> x = 1 + 1e-14
>>> x
1.00000000000001
>>> '%r' % x
'1.00000000000001'
>>> '{0}'.format(x)
'1.0'

【问题讨论】:

    标签: python string python-2.7 formatting


    【解决方案1】:

    啊,在第一次阅读的文档中错过了它;这是!r 转换。 (见examples section in docs

    >>> '{0!r}'.format(x)
    '1.00000000000001'
    

    【讨论】:

      猜你喜欢
      • 2013-03-29
      • 2021-12-29
      • 2010-10-11
      • 1970-01-01
      • 2021-06-19
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多