Код: Выделить всё
template_a = "The current name is {name}"
names = ["foo", "bar"]
for name in names:
print (template_a.format(**locals()))
Код: Выделить всё
names = ["foo", "bar"]
for name in names:
print (f"The current name is {name}")
Есть ли способ ввести строку и интерпретировать ее как f-строку, чтобы избежать использования вызова .format(**locals())?
В идеале я хочу иметь возможность писать такой код... (где Magic_fstring_function — вот где появляется часть, которую я не понимаю):
Код: Выделить всё
template_a = f"The current name is {name}"
# OR [Ideal2] template_a = magic_fstring_function(open('template.txt').read())
names = ["foo", "bar"]
for name in names:
print (template_a)
Код: Выделить всё
The current name is foo
The current name is bar
Код: Выделить всё
The current name is {name}
The current name is {name}
Подробнее здесь: https://stackoverflow.com/questions/424 ... -f-strings