推荐学习书目
› 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
eote
V2EX  ›  Python

Python string 格式化的问题

  •  
  •   eote · Feb 28, 2016 · 3576 views
    This topic created in 3862 days ago, the information mentioned may be changed or developed.

    使用 format 与%格式化字符串有什么区别。为什么我在文档里多用 format 而别人的项目里看到的都是用%
    <script src="https://gist.github.com/imAArtist/116c5ed823fafa593e28.js"></script>

    9 replies  •  2016-02-29 13:02:53 +08:00
    eote
        1
    eote  
    OP
       Feb 28, 2016
    WangYanjie
        2
    WangYanjie  
       Feb 28, 2016
    google
    tcitry
        3
    tcitry  
       Feb 28, 2016
    shyling
        4
    shyling  
       Feb 28, 2016
    写 c 时熟悉了%d
    pc10201
        5
    pc10201  
       Feb 28, 2016
    %格式化字符串是老式的写法
    format 是新式的,而且功能更多
    Kisesy
        6
    Kisesy  
       Feb 28, 2016
    你再学一段时间,就是这个了 log.debug(xxxx)
    Ge4Los
        7
    Ge4Los  
       Feb 28, 2016
    "this is tuple : %s" % (1,2,3) #不能直接使用元组。
    "this is tuple:{}".format((1,2,3)) #可以直接使用元组。
    而 %s 要想输出元组,就得用 ((1,2,3),) 作为参数了
    erse
        8
    erse  
       Feb 28, 2016
    最好用 format 而不是用%,有坑
    feather12315
        9
    feather12315  
       Feb 29, 2016 via Android
    doc 建议 format
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2456 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 00:25 · PVG 08:25 · LAX 17:25 · JFK 20:25
    ♥ Do have faith in what you're doing.