I would like to create an f-string that could be used multiple times as the following code with format
:
TEXT_AMOUNT = 'text {amount}'
def f1(beginning):
return beginning + TEXT_AMOUNT.format(amount=10)
def f2(ending):
return TEXT_AMOUNT.format(amount=100) + ending
How can I achieve the same functionality with using an f-string? I tried:
TEXT_AMOUNT = f'text {amount}'
def f1(beginning):
amount = 100
return beginning + TEXT_AMOUNT
def f2(ending):
amount = 10
return TEXT_AMOUNT + ending
However, I get the following error:
NameError: name 'amount' is not defined
You can store it as lambda function:
TEXT_AMOUNT = lambda amount: f'text {amount}'
print(TEXT_AMOUNT(10))
out: 'text 10'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With