V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cbsw
V2EX  ›  程序员

PEP 0498: Literal String Formatting

  •  
  •   cbsw ·
    dengshuan · 2015-08-09 10:43:13 +08:00 · 3606 次点击
    这是一个创建于 3176 天前的主题,其中的信息可能已经有所发展或是发生改变。

    PEP 0498

    >>> import datetime
    >>> name = 'Fred'
    >>> age = 50
    >>> anniversary = datetime.date(1991, 10, 12)
    >>> f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'
    'My name is Fred, my age next year is 51, my anniversary is Saturday, October 12, 1991.'
    >>> f'He said his name is {name!r}.'
    "He said his name is 'Fred'."
    

    其它语言里面也有,现在Python终于也要加上了,用起来还是很方便的

    11 条回复    2015-09-20 20:58:13 +08:00
    freefcw
        1
    freefcw  
       2015-08-09 10:47:50 +08:00
    这个看着不错,很简洁
    janxin
        2
    janxin  
       2015-08-09 10:49:42 +08:00
    需要3.6,估计要明年才能用上了吧
    Septembers
        3
    Septembers  
       2015-08-09 10:53:46 +08:00 via Android
    @janxin 3.5才发布没多久
    janxin
        4
    janxin  
       2015-08-09 10:58:59 +08:00
    @Septembers 3.5都还没rc,大概3.6要明年了...
    jjx
        5
    jjx  
       2015-08-09 14:04:24 +08:00
    banxi1988
        6
    banxi1988  
       2015-08-09 15:41:20 +08:00
    这语法 ,通过一个Python 特有的前缀 表示法,好像可以少写一个转义字符。
    比如 Ruby 是 "#{}" ,Swift 是"\()"
    kikyous
        7
    kikyous  
       2015-08-09 17:54:13 +08:00
    ruby和coffee都用n年了
    cute
        8
    cute  
       2015-08-10 14:01:41 +08:00
    楼上这么说,php只能呵呵了。
    zonyitoo
        9
    zonyitoo  
       2015-08-10 14:42:13 +08:00
    String Interpolation, 脚本语言实现最方便了
    Jex
        10
    Jex  
       2015-08-11 12:05:14 +08:00   ❤️ 1
    语法有点不一致, Interpolation 直接 Expression 就行了,{name!r} 和 {anniversary:%A, %B %d, %Y} 这种增加了不必要的特殊语法
    justahappy
        11
    justahappy  
       2015-09-20 20:58:13 +08:00
    @cute 专科生用的语言 哪来的优越感????????
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5274 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 05:55 · PVG 13:55 · LAX 22:55 · JFK 01:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.