I have previously used string formatting using format(). I have seen code where % is used for formatting. Recently I started using f-strings and they are more flexible and easy to use than the other above mentioned approaches. f-strings also seemed more generic (for example, you don't need to use an extra method like .format()). The advantages over the other methods are given here in PEP 498.
Why is f-string NOT the default? If it was default, I would not have add f before the string when I use it. I can directly. In other words, what are the pitfalls in using f-string over other string formatting schemes?
It can not be the default behavior because it would totally break existing code like this:
template = "hello {who}"
data = {"who": "world"}
rendered = template.format(**data)
If f-strings were the default you would get:
NameError: name 'who' is not defined
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With