PEP750: Template Strings (new updates)

I personally think so.

I don’t think so as it isn’t a subclass of str.

Or from string import templatelib which also disambiguates everything coming from that module.

4 Likes

Naming precision / readability aside, I just learned that there is already string.Template, which is currently even called “template strings” in the docs. Then introducing another concept “template strings” under the name string.templatelib.Template is a recipe for confusion.

There should be different names in both the class names and in conceptual naming (how to unambiguously call these when speaking about them, something like “template strings” / “$-template strings” / “f-string templates”, … ).

Suggestion:

Conceptual naming for the new templating mechanism: Let’s start with the analogy to f-strings. f”…” is a f-string literal that is evaluated to a str. It’s conventionally called “f-string”. t”…” is formally a t-string literal (or maybe template-string literal) that is evaluated to a string template, which can later be turned into a str. I believe calling t”…” a “template string” in analogy to “f-string” is reasonable (“t-string” would also be possible but is less clear and we also have the precedence for full word prefixes in “raw string” - r”…”). The “template string” is evaluated to a “string template”; class names for that could be StrTemplate or StringTemplate.

The existing string.Template mechanism should then be reworded to “$-string templates” or similar to free the conceptual name “template string” for the new mechanism. I believe we fundamentally want “template string” for the new concept and string.Template is niche enough so that this rewording is possible.

2 Likes

This is the terminology used in the PEP’s Specification section (but not the title), and I believe the reference implementation: https://peps.python.org/pep-0750/#template-string-literals

Perhaps the PEP title could be updated, if deemed valuable?

A

I think colloquially, most people talk about these things as f-strings, t-strings, and $-strings (dollar-strings). There’s not too much confusion about them, although string formatting syntax rivals arg parsing libraries in the stdlib :smiley:

3 Likes

In my experience I don’t think I’ve ever used string.Template and thus would not refer to them by any name. Do for me I don’t think having two things named Template is confusing, since I only expect to see f and t strings. But I do no i18n work.

Have to pile on: there’s absolutely a chance of confusion when there’s already “Template Strings” in Python since all the way back in 2.4, which by the way use a class named Template. Of course code can disambiguate this, not the point. I know not many people use these $-strings (huh, not heard that term before), but they’re all over the internet in (often low quality) tutorials which try to describe all the string formatting approaches “just for completeness”. We’ll have some fun at first when the new breed of AI-first programmers ask itheir IDE about template strings and the Template class, when the body of knowledge includes the previous 20+ years worth of data when that meant something a bit different from what it’s going to mean now. This is just a request to mitigate some confusion by tweaking terminology.

4 Likes