• 请不要在回答技术问题时复制粘贴 AI 生成的内容
cbsw
V2EX  ›  程序员

PEP 0498: Literal String Formatting

  •  
  •   cbsw ·
    dengshuan · Aug 9, 2015 · 4900 views
    This topic created in 4117 days ago, the information mentioned may be changed or developed.

    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 replies  •  2015-09-20 20:58:13 +08:00
    freefcw
    freefcw
    Aug 9, 2015
    1
    这个看着不错,很简洁
    janxin
    janxin
    Aug 9, 2015
    2
    需要3.6,估计要明年才能用上了吧
    Septembers
    Septembers
    Aug 9, 2015 via Android
    3
    @janxin 3.5才发布没多久
    janxin
    janxin
    Aug 9, 2015
    4
    @Septembers 3.5都还没rc,大概3.6要明年了...
    jjx
    jjx
    Aug 9, 2015
    5
    banxi1988
    banxi1988
    Aug 9, 2015
    6
    这语法 ,通过一个Python 特有的前缀 表示法,好像可以少写一个转义字符。
    比如 Ruby 是 "#{}" ,Swift 是"\()"
    kikyous
    kikyous
    Aug 9, 2015
    7
    ruby和coffee都用n年了
    cute
    cute
    Aug 10, 2015
    8
    楼上这么说,php只能呵呵了。
    zonyitoo
    zonyitoo
    Aug 10, 2015
    9
    String Interpolation, 脚本语言实现最方便了
    Jex
    Jex
    Aug 11, 2015❤️1
    10
    语法有点不一致, Interpolation 直接 Expression 就行了,{name!r} 和 {anniversary:%A, %B %d, %Y} 这种增加了不必要的特殊语法
    justahappy
    justahappy
    Sep 20, 2015
    11
    @cute 专科生用的语言 哪来的优越感????????
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2459 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 11:40 · PVG 19:40 · LAX 03:40 · JFK 06:40
    ♥ Do have faith in what you're doing.