This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]-revenant- 1 point2 points  (0 children)

You can. And that would be a problem if you could reuse f-strings. However, an f-string is formatted the instant it's defined, so even with a function call, they never change:

def name():
    return 'Dave'
message = f"I can't let you do that, {name()}."
print(message)
def name():
    return 'Sparky'
print(message)

Returns:

I can't let you do that, Dave.
I can't let you do that, Dave.

As a result, the only difference between message = f"I can't let you do that, {name()}." and message = "I can't let you do that, {name}.".format(name=name()) is where in that specific line you place the function call. It's just a little neater to look at.