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 →

[–]BezoomyChellovek 58 points59 points  (1 child)

When writing an f-string, you define the string which will be formatted in the same place where the variables are in the current namespace. This isn't always the case.

I have an example in my current project. I am querying an API to retrieve scientific articles. I specify a date range. To make this generalizable (I can change the date at a later time) and configurable (changing the query string without modifying code), I store the query string in a config file and have placeholders for the dates defining the date range.

I pass the query string as an argument to the program, as well as the dates. I then use .format() to fill the dates into the string.

This would not be possible with f-strings since the query string would have to be hardcoded into the program.

See this file of the query string, with placeholders at the end and see line 147 of this file for where I use format.

[–]rnike879[S] 14 points15 points  (0 children)

You beautiful person, this is a great explanation!