Custom Strings: h'.....'

What do you think about making “custom strings” possible in Python?

The h-string idea in the abstract sounds intriguing (the choice of “h” may be dubious but let’s not get into that right now), but personally I’m not too excited with the __h__ design. As it stands, it might be easier for libraries to provide h-string semantics by themselves like how future-fstrings works, instead of having the support coded into core Python.


You might want to look at PEP 501, General Purpose String Interpolation. I have a long term goal of looking at it again.


Also @jimbaker has some ideas on this.

1 Like

Last summer I did some exploration with @guido of a rough analogue to JavaScript’s tagged templates for Python in a personal repo, along with some related ideas like fl-strings and Quote objects. Unfortunately I got very busy later in the summer, amplified by the pandemic, so I didn’t have time/energy to look into this topic further.

This exploration can be taken in several directions, and it’s likely worth revisiting. Some points to consider:

  • As I understand it, JS does implement tagged templates and corresponding tag functions using static analysis. This would not be the way we would want to do it in Python! One should be able to define/import/manipulate/mock such tag functions as usual.
  • On the other hand, the use of variables in such strings should not require the use of sys._getframe (as we see with say flufl.i18n). Avoiding this usage was a good design decision for f-strings. Also, lexical scope is better :slight_smile: .
  • Minimizing overhead of the quoting is important. f-strings are popular in part because they are fast, not just because they are also easy to use.

Personally this idea is something I do again have time to look into.

1 Like

@jimbaker thank you for your reply. Will fl-strings be able to do the magic which gets done by Django conditional_escape()?

If you are unsure what I mean, then please have a look at my explanation: GitHub - guettli/python-custom-strings: Python Custom Strings (pre PEP)

Thank you.

Hi David John,

Your question doesn’t seem to have anything to do with the topic, which
is a proposal for a new kind of “h-string” for Python.

Perhaps you should ask your question in the Users category by starting a
new topic.

1 Like

Thank you, PEP 501 looks promising.