Alternatives¶
f-strings. As of Python 3.6, Python finally has formatted strings that are in-place interpolated. Thank you, PEP 498. Some decades after Perl, PHP, Ruby, et al, but bravo! nonetheless. In many cases, if all you need are interplated strings, f-strings are grand. Just one of many reasons to upgrade to the latest modern Python builds. Sadly, f-strings lack the easy coloring, wrapping, and other formatting functions
saybuilds in. But f-strings are quite compatible withsay, so feel free to use them together.ScopeFormatter provides variable interpolation into strings. It is amazingly compact and elegant. Sadly, it only interpolates Python names, not full expressions.
sayhas full expressions, as well as a framework for higher-level printing features beyondScopeFormatter’s…um…scope.interpolate is similar to
say.fmt(), in that it can interpolate complex Python expressions, not just names. Itsi % "format string"syntax is a little odd, however, in the way that it re-purposes Python’s earlier"C format string" % (values)style%operator. It also depends on the nativeprintstatement or function, which doesn’t help bridge Python 2 and 3.Even simpler are invocations of
%orformat()usinglocals(). E.g.:name = "Joe" print "Hello, %(name)!" % locals() # or print "Hello, {name}!".format(**locals())
Unfortunately this has even more limitations than
ScopeFormatter: it only supports local variables, not globals or expressions. And the interpolation code seems gratuitous. Simpler:say("Hello, {name}!")