[issue28904] add more format conversion flags eg. "len" and "id"

Samuel Colvin report at bugs.python.org
Thu Dec 8 09:37:06 EST 2016


New submission from Samuel Colvin:

(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.

----------
components: Library (Lib)
messages: 282707
nosy: Samuel Colvin
priority: normal
severity: normal
status: open
title: add more format conversion flags eg. "len" and "id"
type: enhancement
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28904>
_______________________________________


More information about the Python-bugs-list mailing list