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
eote
V2EX  ›  Python

Python string 格式化的问题

  •  
  •   eote · 2016-02-28 19:11:36 +08:00 · 2544 次点击
    这是一个创建于 2972 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    9 条回复    2016-02-29 13:02:53 +08:00
    eote
        1
    eote  
    OP
       2016-02-28 19:14:34 +08:00
    WangYanjie
        2
    WangYanjie  
       2016-02-28 19:15:33 +08:00
    google
    tcitry
        3
    tcitry  
       2016-02-28 19:53:05 +08:00
    shyling
        4
    shyling  
       2016-02-28 19:57:29 +08:00
    写 c 时熟悉了%d
    pc10201
        5
    pc10201  
       2016-02-28 20:09:46 +08:00
    %格式化字符串是老式的写法
    format 是新式的,而且功能更多
    Kisesy
        6
    Kisesy  
       2016-02-28 20:27:56 +08:00
    你再学一段时间,就是这个了 log.debug(xxxx)
    Ge4Los
        7
    Ge4Los  
       2016-02-28 20:58:59 +08:00
    "this is tuple : %s" % (1,2,3) #不能直接使用元组。
    "this is tuple:{}".format((1,2,3)) #可以直接使用元组。
    而 %s 要想输出元组,就得用 ((1,2,3),) 作为参数了
    erse
        8
    erse  
       2016-02-28 21:13:57 +08:00
    最好用 format 而不是用%,有坑
    feather12315
        9
    feather12315  
       2016-02-29 13:02:53 +08:00 via Android
    doc 建议 format
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5161 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:43 · PVG 13:43 · LAX 22:43 · JFK 01:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.