字符串占位符的更Pythonic方式?

Whi*_*ale 0 python

有没有更Pythonic的方法来执行以下操作?F 字符串似乎需要一个定义的变量(没有空表达式),但如果我想稍后定义 @names 和 @locations,最好的方法是什么?

funct_a = call_function()

str_a = f"a very long string of text that contains {funct_a} and also @names or @locations"

... 
large chunk of code that modifies str_a and defines var_a, var_b, var_c, var_d
...

if <conditional>:
    str_b = str_a.replace("@names", var_a).replace("@locations", var_b)
elif <conditional>:
    str_b = str_a.replace("@names", var_c).replace("@locations", var_d)
Run Code Online (Sandbox Code Playgroud)

geo*_*org 5

{}从 f 字符串中转义's 并稍后在 中使用它们format

now = 'hey'

s = f'{now}, then {{names}} or {{locations}}'

# later on

print(s.format(names='foo', locations='bar'))
Run Code Online (Sandbox Code Playgroud)

注意:如果直接扩展也包含{}.