Is it possible to reuse an f-string as it is possible with a string and format?
You can store it as lambda function:
TEXT_AMOUNT = lambda amount: f'text {amount}'
print(TEXT_AMOUNT(10))
out: 'text 10'
You can't.
An f-string isn't a kind of string, it's a kind of string literal, which is evaluated immediately. You can't store an f-string in a variable to be evaluated later, or accept one from a user, etc.1 This is the only reason that they're safe.
So, what if you do want to use a format multiple times (or one taken from a user, etc.)? You use str.format
.
Occasionally, you need to capture all of the locals and globals the same way an f-string does, but to do it explicitly. Because this is a rare case (and potentially a security hole), it's intentionally a bit ugly:
TEXT_AMOUNT = 'text {amount}'
def f1(beginning):
amount = 100
return beginning + TEXT_AMOUNT.format(**locals(), **globals())
This makes you think about what you're writing—you don't really want globals
here, right? So leave it off. And it also signals the reader—if you're pulling in locals
, they'll want to see that the string really is a constant in your source that isn't doing anything dangerous.
1. Well, you could use an f-string inside a string that you pass to eval
… but that's a terrible idea.