Nested f-strings

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

    Any basic use case is where you need a string to completely describe the object you want to put inside the f-string braces {}. For example, you need strings to index dictionaries.

    So, I ended up using it in an ML project with code like:

    scores = dict()
    scores[f'{task}_accuracy'] = 100. * n_valid / n_total
    print(f'{task}_accuracy: {scores[f"{task}_accuracy"]}')
    

提交回复
热议问题