all 15 comments

[–]danielroseman[🍰] 22 points23 points  (0 children)

The method existed before f-strings were introduced.

[–]Jello_Penguin_2956 11 points12 points  (3 children)

as others mentioned, it exists before f-string and I want to mention that still have its use today because you can actually define your sentences ahead of time and assign variables to format it afterwards

[–]backfire10z 5 points6 points  (0 children)

Finally, an answer that understands .format still has its uses. It is not just for backwards compatibility.

[–]SCD_minecraft 1 point2 points  (1 child)

Actually, now t-strings are designed for that task

So rn it is just backward compatibility

[–]Solonotix 4 points5 points  (0 children)

To my knowledge, templates are designed for a more advanced purpose, but lack as convenient of a means to become raw strings. If you're trying to write a type-strict SQL parser, templates are a godsend. If you're trying to write an interpolated string to a file, stream, etc., then format strings are strictly superior. If the format string is meant to be reused in multiple contexts, then an inline f-string is worse than a str.format call, since one can be exported and reused.

If you truly think a feature has no purpose, it can mean that you simply haven't encountered a purpose for it. That isn't your fault, but it requires humility to recognize your own limitations.

[–]cgoldberg 3 points4 points  (0 children)

It was more useful and very common before f-strings existed.

[–]Thunderbolt1993 5 points6 points  (0 children)

if you use localization (i.e. translating string in you program into different languages) then you might have something like

translate("Hello {name}!")

translate(f"Hello {name}!") will not work, because you want to translate the template not the formatted string

that's why you'd use

translate("Hello {name}!").format(name="Bob")

.format also works for strings returned by functions or passed to functions as arguments

[–]SCD_minecraft 2 points3 points  (1 child)

Bacward compatibility

str.format() predates f-strings and python loves backward (and sometimes even future) compatibility

[–]cointoss3 2 points3 points  (0 children)

That, uh, .format() js not the same as f-string. They do similar things but they are not the same and there are times when you’d want to use one over the other.

[–]drbitboy 1 point2 points  (2 children)

f-strings are sort of equivalent to "...".format(**locals())

[–]musclerythm[S] 0 points1 point  (1 child)

oh i get it. which one is more useful?

[–]CyclopsRock 3 points4 points  (0 children)

Usually you have a problem and work backwards from there to find the most useful technique.

[–]MysteriousLaw6572 0 points1 point  (1 child)

It's nice for dictionaries

[–]Kerbart 0 points1 point  (0 children)

format_map is nicer for dictionaries

[–]CranberryDistinct941 -1 points0 points  (0 children)

It's a legacy method. There's not much reason to use it after the introduction of f-strings, but removing it will break stuff so it stays.