0

I already saw multiple threads about postponed usage of f-strings, but they are all relatively old.

So here are some examples I found:

# lambda-function as constant
POSTPONED_F_STRING = lambda x, y, z: f"Use variables: {x}, {y}, {z}"

# function/method
def fstr(template):
    return eval(f"f'{template}'")

Or is it even better to use the old str.format() in this case? Any new stuff I didn't mention?

I would really like to here some input on the "right" way to do this, if there even is one.

Thanks in advance!

JulMai
  • 15
  • 3
  • 3
    Yes just use `str.format` – mousetail Sep 01 '22 at 09:33
  • 1
    Simply calling `str.format` at the appropriate time seems the most straightforward. However, opinion-based questions are not particularly encouraged on SO, so you may want to narrow down the criteria or context. – Lev Levitsky Sep 01 '22 at 09:34

1 Answers1

1

str.format is your best option, yes, for the same syntax as an f-string.

eval is evil, as we should all know:

>>> fstr("' + __import__('os').system('uname') + '")
Darwin
AKX
  • 152,115
  • 15
  • 115
  • 172