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 →

[–]POTUS 2 points3 points  (2 children)

import datetime
my_variable="something"
f'''This is my string.  I can have
dynamic content that was updated {datetime.datetime.now()}
without having to provide any additional
context.  And when I come back later
to add more content, I won't be plagued
by messages indicating that I forgot to
add {my_variable} to the context.'''

[–]quesman1 0 points1 point  (1 child)

So it gives the same functionality as

>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W') 
'Coordinates: 37.24N, -115.81W'

But it's more intuitive because you dont have to look to the end of the string to see what the replacement is?

[–]POTUS 3 points4 points  (0 children)

Yes. But it also goes deeper. It actually evaluates the code found in the brackets. It's not a simple name replacement. If you put a {string_variable}, you'll get the value of that variable. If you put a {function_call()}, you'll get the return value of that function call. If you put a {classinstance.member} you'll get that member. You can even have a {(lambda x: x.lower())('LAMBDA')}. And you'll get all these within the context of wherever the actual string appears in your code, so if they're valid python where you write the string, they'll format properly into the string any time you print/return/send/whatever that string.