>>> "Hello {name}".format(name="World")
'Hello World'
But the string.format method only superficially resembles fstrings, no? Itâs much simpler.
Thatâs correct, but like you already said why would you want to evaluate (potentially) untrusted code? Eval really is dangerous | Ned Batchelder
There would also be no way to check for syntax errors in advance.
Yes, I kind of realized that. The fact that fstrings really can include any code de facto means that theyâre not suitable for âexternal loadingâ, and the same goes for tstrings. I guess str.format() or a dedicated templating package like Jinja are better alternatives for what I had in mind. Thanks
A question: what will be the advantage of using a Template string for SQL vs prepared statements?
Itâs similar to the benefit f-strings offer over using string formatting on literal strings: the reader doesnât have to mentally interpolate the values into their corresponding locations.
Thereâs also a slight performance advantage, since the template gets parsed into text segments and interpolation fields at compile time rather than runtime (again similar to f-strings vs dynamic string formatting).
https://docs.python.org/3/library/string.html#custom-string-formatting is the stdlib tool for processing format strings at runtime.
With PEP 750, itâs feasible to write a custom formatter that produces a template object placeholder field values from a regular string.
Apologies in advance, I havenât been following this closely.
But hopefully it is a straight forward question.
I see that string-prefixes are being specialised for formatting only.
However, will it be possible to get the raw string?
as per example in jupyterlite
:
def greet(*args):
print(args)
greet"Hello ?? {a:1}" # (DecodedConcrete('Hello ?? '), InterpolationConcrete(...))
However, what if I donât need to format, all I need is syntactic convenience. I.e.:
def greet(string):
print(string)
greet"Hello ?? {a:1}" # "Hello ?? {a:1}"
Wouldnât it be good (if it is not possible by current plan) to not limit this to string formatting, but have a more general pass-through layer first, which is then specialised to string formatting?
The latest proposal completely dropped the custom tag syntax. The PEP should be updated and a new thread created, continuing this one is just going to cause more and more confusion.
Yes please. When I saw there were over 300 posts I started to panic.
Weâre currently doing some more minor changes to the PEP. Thereâll be a new version out soon, along with a new Discourse thread.
Weâve just posted an update to PEP 750 and (phew!) a new Discourse thread.
Hop on in!