Nested f-strings

后端 未结 6 1993
礼貌的吻别
礼貌的吻别 2020-11-30 07:10

Thanks to David Beazley\'s tweet, I\'ve recently found out that the new Python 3.6 f-strings can also be nested:

>>> price = 478.23
>>> f\"         


        
6条回答
  •  时光取名叫无心
    2020-11-30 07:44

    I guess this is to pass formatting parameters in the same line and thus simplify f-strings usage.

    For example:

    >>> import decimal
    >>> width = 10
    >>> precision = 4
    >>> value = decimal.Decimal("12.34567")
    >>> f"result: {value:{width}.{precision}}"
    'result:      12.35'
    

    Of course, it allows programmers to write absolutely unreadable code, but that's not the purpose :)

提交回复
热议问题