V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Nov 13, 2017 · 4895 views
    This topic created in 3098 days ago, the information mentioned may be changed or developed.
    {!r}是指以什么样的格式输出呢?
    谢谢了!
    5 replies    2017-11-15 20:50:40 +08:00
    ipwx
        1
    ipwx  
       Nov 13, 2017
    repr(x)
    enrolls
        2
    enrolls  
       Nov 14, 2017   ❤️ 1
    善用谷歌啊。https://pyformat.info/
    zhusimaji
        3
    zhusimaji  
       Nov 14, 2017 via iPhone
    是时候了解一下 repr 与 str 区别了
    bxtx999
        4
    bxtx999  
       Nov 14, 2017
    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
       Nov 15, 2017
    @ipwx
    @enrolls
    @zhusimaji
    @bxtx999 感谢大家指点!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5394 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 08:05 · PVG 16:05 · LAX 01:05 · JFK 04:05
    ♥ Do have faith in what you're doing.