LWN: Comments on "Easier Python string formatting" https://lwn.net/Articles/656898/ This is a special feed containing comments posted to the individual LWN article titled "Easier Python string formatting". en-us Wed, 22 Oct 2025 06:04:32 +0000 Wed, 22 Oct 2025 06:04:32 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net Easier Python string formatting https://lwn.net/Articles/658082/ https://lwn.net/Articles/658082/ Cyberax <div class="FormattedComment"> Uhm, how? You still have to encode/decode incoming data.<br> </div> Tue, 22 Sep 2015 17:14:59 +0000 Easier Python string formatting https://lwn.net/Articles/658041/ https://lwn.net/Articles/658041/ MKesper <div class="FormattedComment"> decode() and .encode() clutter is what I remove when converting from Py2 to Py3.<br> </div> Tue, 22 Sep 2015 10:42:02 +0000 Easier Python string formatting https://lwn.net/Articles/657633/ https://lwn.net/Articles/657633/ Rynor <div class="FormattedComment"> This is just silly and so contradictory to the Zen of Python.<br> I'd say .format() already handles all the required functionality in a concise way.<br> <p> "Explicit is better than implicit."<br> Using variables from the local and global scope is just asking for unclear code.<br> <p> "There should be one-- and preferably only one --obvious way to do it."<br> As said before, there are already too many alternatives. Why add another that just duplicates functionality?<br> <p> And finally..<br> "Special cases aren't special enough to break the rules."<br> </div> Thu, 17 Sep 2015 07:59:41 +0000 Easier Python string formatting https://lwn.net/Articles/657265/ https://lwn.net/Articles/657265/ epa <div class="FormattedComment"> Many 'proper placeholder systems' end up working by string interpolation in the end (I am thinking of DBMSes which don't have a separate wire protocol for declaring and setting placeholders). As long as the interpolation is done sanely and with knowledge of the proper syntax rules, what's the problem?<br> </div> Mon, 14 Sep 2015 08:38:58 +0000 Easier Python string formatting https://lwn.net/Articles/657176/ https://lwn.net/Articles/657176/ Cyberax <div class="FormattedComment"> I guess the same way. You just don't need to specify all the inputs in the .format() part.<br> <p> Actually, a couple of years back, I had a helper that used "locals()" method to get all the in-scope variables for the format() call. <br> </div> Fri, 11 Sep 2015 21:49:26 +0000 Easier Python string formatting https://lwn.net/Articles/657170/ https://lwn.net/Articles/657170/ mgedmin <div class="FormattedComment"> This raises a good point. I know that<br> <p> _("Today is {day} {month}").format(...)<br> <p> works. How will you translate f-strings?<br> </div> Fri, 11 Sep 2015 21:01:51 +0000 Easier Python string formatting https://lwn.net/Articles/657107/ https://lwn.net/Articles/657107/ jezuch <div class="FormattedComment"> <font class="QuotedText">&gt; i'The answer is {answer}'</font><br> &gt;<br> <font class="QuotedText">&gt; There is a key difference, though: while f-strings produce a formatted string immediately on execution, i-strings delay that formatting. An explicit call to a format function is required to do the job.</font><br> <p> Groan. It's not fixing the insecurity, it's delaying the insecurity. Either use proper placeholder system (whatever it is in your language/library) or go back to PHP :)<br> </div> Fri, 11 Sep 2015 11:53:27 +0000 Easier Python string formatting https://lwn.net/Articles/657089/ https://lwn.net/Articles/657089/ vapier <div class="FormattedComment"> i don't generally consider them the same since they have different syntax. these are not equiv:<br> <p> d = {'answer': 1}<br> 'The answer = {answer}'.format(**answer)<br> 'The answer = %(answer)d' % answer<br> <p> the first form has to be:<br> <p> 'The answer = {answer!d}'.format(**answer)<br> <p> and the python team themselves document them in completely different sections:<br> <p> <a href="https://docs.python.org/2/library/stdtypes.html#string-formatting-operations">https://docs.python.org/2/library/stdtypes.html#string-fo...</a><br> <a href="https://docs.python.org/2/library/string.html#format-string-syntax">https://docs.python.org/2/library/string.html#format-stri...</a><br> </div> Thu, 10 Sep 2015 23:36:12 +0000 Easier Python string formatting https://lwn.net/Articles/657085/ https://lwn.net/Articles/657085/ flussence <div class="FormattedComment"> That snide jab at Perl at the end is heavily ironic. Python's newly added «f""» modifier is identical in function and syntax to Perl 6's «q:c""», which predates it by several years. The only real difference is in implementation: Python's is hardcoded into the language, whereas Perl's is extensible and allows user-defined constructs (say, «q:sql""»).<br> <p> («i""» sounds like a 1:1 copy of Javascript's template strings, FWIW.)<br> </div> Thu, 10 Sep 2015 22:42:22 +0000 Easier Python string formatting https://lwn.net/Articles/657080/ https://lwn.net/Articles/657080/ maney <div class="FormattedComment"> There's "explicit is better than implicit", though I would agree that that's more of a guideline than a rule. But my immediate reaction from partway through the article, as shared in #chipy, looked like this:<br> <p> There should be one obviously correct way to do things. Except for string formatting, where three is okay, but four would be better.<br> <p> </div> Thu, 10 Sep 2015 22:25:52 +0000 Easier Python string formatting https://lwn.net/Articles/657077/ https://lwn.net/Articles/657077/ Cyberax <div class="FormattedComment"> However, let's consider this:<br> <p> "Today is {} {}".format(month, date)<br> <p> Now, let's add some localization:<br> <p> "Heute ist {} {}".format(date, month)<br> <p> See the difference? String interpolation is a very useful tool.<br> </div> Thu, 10 Sep 2015 22:08:59 +0000 Easier Python string formatting https://lwn.net/Articles/657075/ https://lwn.net/Articles/657075/ corbet Whatever. I would posit that <p> <pre> 'The answer is {}'.format(answer) </pre> <p> is still more verbose than: <p> <pre> f'The answer is {answer}' </pre> <p> The people working on this facility would seem to agree. But a couple of folks have said this now, so perhaps this feeling isn't universal? Thu, 10 Sep 2015 21:38:28 +0000 Easier Python string formatting https://lwn.net/Articles/657072/ https://lwn.net/Articles/657072/ Fats But your .format() example then really looks as verbose on purpose. <br />You could also do: <pre> 'The answer is {}'.format(42)</pre> Thu, 10 Sep 2015 21:24:08 +0000 Easier Python string formatting https://lwn.net/Articles/657058/ https://lwn.net/Articles/657058/ josh <div class="FormattedComment"> <font class="QuotedText">&gt; The format() string method is more flexible, but is somewhat verbose</font><br> <p> .format() doesn't need to be any more verbose than the % operator, if you don't use names:<br> <p> 'The answer = {}'.format(42)<br> </div> Thu, 10 Sep 2015 17:28:21 +0000 Easier Python string formatting https://lwn.net/Articles/657055/ https://lwn.net/Articles/657055/ dashesy There are already plenitude of excellent template magic in Python, like Jinja and django has its own. Why is the language bothering with providing an (unavoidably) inferior solution to what is just a `pip install` away. The best way to do `foo()` in Python is this: <pre> pip install foo_bar </pre> then <pre> from foo_bar import foo foo() </pre> As if it is not already annoying in one code-base to see strings with single(`), or double(") or tiple(""") quotes (I know PEP discourages some forms), along with <b>b</b> or <b>r</b> or <b>u</b> and now <b>f</b> too! Scattered with `.decode(..)` and `.encode(..)` clutter to make Py3 happy. Thu, 10 Sep 2015 17:04:05 +0000 Easier Python string formatting https://lwn.net/Articles/657046/ https://lwn.net/Articles/657046/ talex <div class="FormattedComment"> A similar system is used in E, except that rather than escaping bad characters, it typically creates a structured object instead of a string. Consider this E code:<br> <p> def name := "O'No! $@?"<br> sql`INSERT INTO users (userName, karma) VALUES ($name, 0)`<br> <p> Here, the template is first parsed as an SQL prepared statement, then the arguments are filled in. There's nothing special about the "sql" prefix. The E parser expands this to:<br> <p> sql__quasiParser.valueMaker("INSERT INTO users (userName, karma) VALUES (${0}, 0)").substitute([name])<br> <p> sql__quasiParser is an ordinary object (in this case, the result of opening a database connection). By creating objects with such names, any prefix can be defined. Similar parsers can be used for XML, regular expressions, JSON, etc.<br> <p> There's no risk of using plain string interpolation by mistake, because something that wants XML will expect an XML element, not a string.<br> <p> See: <a href="http://wiki.erights.org/wiki/Walnut/Ordinary_Programming/Quasi-Literals_and_Quasi-Parsers">http://wiki.erights.org/wiki/Walnut/Ordinary_Programming/...</a><br> </div> Thu, 10 Sep 2015 16:07:06 +0000 Easier Python string formatting https://lwn.net/Articles/657036/ https://lwn.net/Articles/657036/ corbet I had just grouped that in as a use of the <tt>%</tt> operator — didn't want to get into all of the details of it. Especially if you start to get into "<tt>%locals()</tt>" and other dirty tricks like that... Thu, 10 Sep 2015 14:45:03 +0000 Easier Python string formatting https://lwn.net/Articles/657034/ https://lwn.net/Articles/657034/ richmoore <div class="FormattedComment"> There's another existing way too:<br> <p> 'the answer is %(answer)s' % ({'answer': 42})<br> </div> Thu, 10 Sep 2015 14:03:32 +0000