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

字典表的%字符问题

  •  
  •   182247236 · 2016-12-21 17:21:44 +08:00 · 1858 次点击
    这是一个创建于 2683 天前的主题,其中的信息可能已经有所发展或是发生改变。

    遇到个例子

    phonebook
    {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
    "Cecil's phone number is %(Cecil)s." % phonebook
    "Cecil's phone number is 3258." 原文如下: 在每个转换( conversion specifier )中的 % 字符后面,可以加上用圆括号括起来的键,后面再跟上其他说明元素。

    这段有点看不懂,自己理解

    "%( k ) s"%d 'v' 这里的%和 s 代表什么意思?

    4 条回复    2016-12-22 00:43:17 +08:00
    Sylv
        1
    Sylv  
       2016-12-21 18:29:16 +08:00 via iPhone
    关键词: Python 字符串 格式化
    AyoCross
        2
    AyoCross  
       2016-12-21 20:59:50 +08:00
    "Cecil's phone number is %s." % phonebook[Cecil] # 这样会不会更好理解一点
    182247236
        3
    182247236  
    OP
       2016-12-22 00:42:56 +08:00
    @AyoCross get 到一点了,不过 @Sylv 给了关键词,我还是查查
    182247236
        4
    182247236  
    OP
       2016-12-22 00:43:17 +08:00
    @Sylv 马上去查查
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1144 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 18:37 · PVG 02:37 · LAX 11:37 · JFK 14:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.