←back to thread

620 points tambourine_man | 1 comments | | HN request time: 0.21s | source
Show context
avereveard ◴[] No.43749173[source]
what's the tldr difference between this and .format ?
replies(4): >>43749190 #>>43749387 #>>43749636 #>>43750013 #
OJFord ◴[] No.43750013[source]
f-strings are syntactic sugar for .format, e.g.:

    f"foo is {foo} and {bar=}"
    "foo is {} and bar={}".format(foo, bar)
are equivalent.

t-strings are actually not strings, but Template objects, giving access to both the templating string and the parameters for processing. Sibling comments describe it as a custom .format implementation in that sense - it's f-string-like sugar where you're also allowed to take control of the .format function that it's sugar for.

replies(1): >>43757322 #
1. zahlman ◴[] No.43757322[source]
f-strings are actually translated into formatting and concatenation of the pieces at a bytecode level; they don't call `.format` under the hood and thus aren't what I'd call "syntactic sugar" in the traditional sense. But yes, the equivalence holds.

t-strings, of course, (will) translate at the bytecode level into instantiation of the Template object (and whatever code is needed to compute the Interpolation values in-place).