This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: add more format conversion flags eg. "len" and "id"
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, samuelcolvin
Priority: normal Keywords:

Created on 2016-12-08 14:37 by samuelcolvin, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg282707 - (view) Author: Samuel Colvin (samuelcolvin) * Date: 2016-12-08 14:37
(The "Components" selection might be wrong, I wasn't sure what to use)

As https://docs.python.org/3.6/library/string.html

> Three conversion flags are currently supported: '!s' which calls str() on the value, '!r' which calls repr() and '!a' which calls ascii().

It would be great if further conversation flags were added for more built-in methods. In particular:
* '!l' for len()
* '!i' for id()

(There might be others but these are the two I would find most useful.)

format() is now very powerful, but it's very common for strings, lists, dicts, sets etc. to want to include their length in strings. This is currently ugly.

This enhancement will be even more sought after with string literals in python 3.6.

Example of when this would be very useful:

v = {'type': 'whatever, 'items': ['foo', 'bar', 'spam']}
'{type}{items!l}'.format(**v)

Would also be very useful with getattr, and getitem usage.
msg282708 - (view) Author: Samuel Colvin (samuelcolvin) * Date: 2016-12-08 14:40
I know contributing to python is currently a pain (bring on github!) but I'd be happy to attempt a patch if others agree this would be useful.
msg282709 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-12-08 15:20
Thanks for the idea, but I think we're going to see a shift away from format calls and toward fstrings, so I don't think this is worth doing.  See https://www.python.org/dev/peps/pep-0498/#id45; the ! expressions are only supported by fstrings only for consistency with format, not because they are particularly useful with fstrings.
msg282710 - (view) Author: Samuel Colvin (samuelcolvin) * Date: 2016-12-08 15:25
I see, I hadn't appreciated fstrings where entirely different and more powerful than format(), I'll close this.

For anyone else coming to this, with fstrings in >=3.6 you can do:

In [4]: value = [1,2, 3]

In [5]: f'The value is {value}.'
Out[5]: 'The value is [1, 2, 3].'

In [6]: f'The value is {len(value)}.'
Out[6]: 'The value is 3.'
History
Date User Action Args
2022-04-11 14:58:40adminsetgithub: 73090
2016-12-08 15:34:37berker.peksagsetresolution: fixed -> rejected
stage: resolved
2016-12-08 15:25:12samuelcolvinsetstatus: open -> closed
resolution: fixed
messages: + msg282710
2016-12-08 15:20:24r.david.murraysetnosy: + r.david.murray
messages: + msg282709
2016-12-08 14:40:39samuelcolvinsetmessages: + msg282708
2016-12-08 14:37:06samuelcolvincreate