Nested f-strings

后端 未结 6 2016
礼貌的吻别
礼貌的吻别 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:40

    You could use it for dynamicism. For instance, say you have a variable set to the name of some function:

    func = 'my_func'
    

    Then you could write:

    f"{f'{func}'()}" 
    

    which would be equivalent to:

    '{}'.format(locals()[func]()) 
    

    or, equivalently:

    '{}'.format(my_func())
    

提交回复
热议问题