f lines are code. Not only in the safe, βof course, a string literal is a codeβ, but also a dangerous, arbitrary way to execute code. This is a valid string f:
f"{__import__('os').system('install ransomware or something')}"
and he will execute arbitrary shell commands when evaluating.
You ask how to take a line loaded from a text file and evaluate it as code, and the answer comes down to eval . This, of course, is a security risk and probably a bad idea , so I recommend not trying to load f-lines from files.
If you want to load f-string f"My name is {name} and I am {age} years old" from the file, then actually put
f"My name is {name} and I am {age} years old"
in the file, f and quotation marks are all.
Read it from the file, compile it and save it (so eval does not need to recompile it every time):
compiled_fstring = compile(fstring_from_file, '<fstring_from_file>', 'eval')
and evaluate it with eval :
formatted_output = eval(compiled_fstring)
If you do this, be very careful with the sources from which you will load your f-lines.