I was wondering if there is a advantage of using template strings instead of the new advanced string formatting?
-
2Here's one that is hard to do with template strings stackoverflow.com/questions/11388180– John La RooyCommented Jul 24, 2012 at 11:54
-
2Good question (+1). Interestingly enough, I had never seriously read the portion of the docs on template strings before now ...– mgilsonCommented Jul 24, 2012 at 12:27
6 Answers
One key advantage of string templates is that you can substitute only some of the placeholders using the safe_substitute
method. Normal format strings will raise an error if a placeholder is not passed a value. For example:
"Hello, {first} {last}".format(first='Joe')
raises:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'last'
But:
from string import Template
Template("Hello, $first $last").safe_substitute(first='Joe')
Produces:
'Hello, Joe $last'
Note that the returned value is a string, not a Template
; if you want to substitute the $last
you'll need to create a new Template
object from that string.
-
11
-
Template("Hello, $first $last").safe_substitute(first='Joe') produces just nothing. You have to print it. :) Commented Jan 8, 2018 at 17:59
-
can't see this as an advantage. surely it's better to handle the exception?– jonathanCommented Mar 15, 2018 at 8:27
-
7It could be an advantage in a situation where one wants to perform two layers of templating, i.e. where one wants to generate a template string from another template string, and to defer the remaining substitutions to a second template. I just ran into such a situation, in a code review, where someone had reasons for performing template substitutions incrementally. Commented Jan 13, 2020 at 22:37
-
2Very useful for templating regex patterns where the
{variant_text}_\d{4}
type of syntax is used, as the typicalstr.format()
function wants a value for the '4'. This solves that issue. Would give 10 upvotes if I could.– s3devCommented May 28, 2020 at 10:49
Templates are meant to be simpler than the the usual string formatting, at the cost of expressiveness. The rationale of PEP 292 compares templates to Python's %
-style string formatting:
Python currently supports a string substitution syntax based on C's
printf()
'%' formatting character. While quite rich, %-formatting codes are also error prone, even for experienced Python programmers. A common mistake is to leave off the trailing format character, e.g. thes
in%(name)s
.In addition, the rules for what can follow a % sign are fairly complex, while the usual application rarely needs such complexity. Most scripts need to do some string interpolation, but most of those use simple "stringification" formats, i.e.
%s
or%(name)s
This form should be made simpler and less error prone.
While the new .format()
improved the situation, it's still true that the format string syntax is rather complex, so the rationale still has its points.
-
2So the only advantage of string templates is that it's less complex?– P3trusCommented Jul 24, 2012 at 13:06
-
7@P3trus: Basically, yes. Sometimes the different metacharacters might also fit different situations. If you, say, write a snippet of C code to a file with some strings replaced, a template string might be preferable since you don't need to double all braces to escape them, and since
$
does not have any special meaning in C. Commented Jul 24, 2012 at 13:12 -
7Template strings can be more secure realpython.com/python-string-formatting/… Commented Jan 14, 2020 at 8:51
For what it's worth, Template substitution from a dict appears to be 4 to 10 times slower than format substitution, depending on the length of the template. Here's a quick comparison I ran under OS X on a 2.3 GHz core i7 with Python 3.5.
from string import Template
lorem = "Lorem ipsum dolor sit amet {GIBBERISH}, consectetur adipiscing elit {DRIVEL}. Expectoque quid ad id, quod quaerebam, respondeas."
loremtpl = Template("Lorem ipsum dolor sit amet $GIBBERISH, consectetur adipiscing elit $DRIVEL. Expectoque quid ad id, quod quaerebam, respondeas.")
d = dict(GIBBERISH='FOOBAR', DRIVEL = 'RAXOOP')
In [29]: timeit lorem.format(**d)
1.07 µs ± 2.13 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [30]: timeit loremtpl.substitute(d)
8.74 µs ± 12.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
The worst case I tested was about 10 times slower for a 13 character string. The best case I tested was about 4 times slower for a 71000 character string.
-
I think its just typo, but In[29] have 1000000 loops and In[30] have 100000 loops. please confirm.– sandeepCommented Jun 20, 2018 at 8:18
-
4@sandeep It's not a typo. The ipython timeit command is smart. It runs only as many loops as needed to get an accurate estimate. The template version is about an order of magnitude slower and timeit needed correspondingly fewer loops to reach a result. Commented Jun 20, 2018 at 12:58
Its primarily a matter of syntax preference, which usually boils down to a laziness/verbosity tradeoff and familiarity/habits with existing string template systems. In this case template strings are more lazy/simple/quick to write, while .format()
is more verbose and feature-full.
Programmers used to the PHP language or the Jinja family of template systems may prefer template strings. Using "%s" positional style tuple substitution might appeal to those who use printf
-like string formatting or want something quick. .format()
has a few more features, but unless you need something specific that only .format()
provides, there is nothing wrong with using any existing scheme.
The only thing to be aware of is that named string templates are more flexible and require less maintenance than order-dependent ones. Other than that it all comes down to either personal preference or the coding standard of the project you are working on;
-
1@P3trus You misunderstand me. I was saying that although both methods provide ordered and named fields as a feature, named fields are more flexible and robust to change, regardless of the templating API you use; it was a value judgement on a feature common to both methods, not on the methods themselves. Commented Jul 24, 2012 at 15:32
-
I can't see when templates would be less verbose than format(), e.g.
string.Template('bob is $age years old').substitute(age=age)
vs'bob is {age} years old'.format(age)
– danioCommented Feb 1, 2017 at 12:20
This is old but it is worth mentioning that one great advantage of using Template Strings is that it is safe when you are accepting the template from un-trusted source. This can be from a configuration file for instance.
Here is an example from this article:
CONFIG = {"SECRET_KEY": "super secret key"}
class Event:
def __init__(self, id_, level, message):
self.id_ = id_
self.level = level
self.message = message
def format_event(format_string, event):
return format_string.format(event=event)
# user supplied template
inp = "{event.__init__.__globals__[CONFIG][SECRET_KEY]}"
event = Event(1234, "foo", "boo")
print(format_event(inp, event))
output:
super secret key
Adding to the other answers: one advantage of template strings vs. format strings is that often, the characters {}
are more likely to naturally occur in a piece of text than $foo
or ${foo}
(e.g. when generating source code). That means that with templates, you need to spend less effort in escaping these occurrences of proper text.