V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
saximi
V2EX  ›  Python

请问这个语句是输出什么? print('{!r}'.format(x))

  •  
  •   saximi · 2017-11-13 22:16:45 +08:00 · 4072 次点击
    这是一个创建于 2568 天前的主题,其中的信息可能已经有所发展或是发生改变。
    {!r}是指以什么样的格式输出呢?
    谢谢了!
    5 条回复    2017-11-15 20:50:40 +08:00
    ipwx
        1
    ipwx  
       2017-11-13 23:30:35 +08:00
    repr(x)
    enrolls
        2
    enrolls  
       2017-11-14 02:43:14 +08:00   ❤️ 1
    善用谷歌啊。https://pyformat.info/
    zhusimaji
        3
    zhusimaji  
       2017-11-14 08:40:39 +08:00 via iPhone
    是时候了解一下 repr 与 str 区别了
    bxtx999
        4
    bxtx999  
       2017-11-14 09:07:29 +08:00
    The str() function is meant to return representations of values which are fairly human-readable, while repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError if there is not equivalent syntax). For objects which don ’ t have a particular representation for human consumption, str() will return the same value as repr().

    '!a' (apply ascii()), '!s' (apply str()) and '!r' (apply repr()) can be used to convert the value before it is formatted:

    >>> 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.
    saximi
        5
    saximi  
    OP
       2017-11-15 20:50:40 +08:00
    @ipwx
    @enrolls
    @zhusimaji
    @bxtx999 感谢大家指点!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3587 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 97ms · UTC 10:58 · PVG 18:58 · LAX 02:58 · JFK 05:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.