【发布时间】:2016-11-19 23:02:45
【问题描述】:
根据Python 2.7.12 documentation:
!s(应用str())和!r(应用repr())可用于转换 格式化前的值。>>> import math >>> print 'The value of PI is approximately {}.'.format(math.pi) The value of PI is approximately 3.14159265359. >>> print 'The value of PI is approximately {!r}.'.format(math.pi) The value of PI is approximately 3.141592653589793.
有趣的是,转换后的值是repr() 的输出,而不是str()。
>>> str(math.pi)
'3.14159265359'
>>> repr(math.pi)
'3.141592653589793'
那么这里的“转换值”是什么意思呢?让它不那么可读?
【问题讨论】:
-
观察
str(..)和repr(..)的不同精度很有趣
标签: python string python-2.7 formatting repr