[Python-ideas] Custom converters in str.format() and f-strings

Eric V. Smith eric at trueblade.com
Sat May 4 15:14:22 EDT 2019


I think PEP 501 would be a better approach to this. 

Eric

> On May 4, 2019, at 11:51 AM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> 
> Currently str.format() and f-strings support three converters. Converting is specified by the "!" character followed by a letter which denotes the converter.
> 
> s: str()
> r: repr()
> a: ascii()
> 
> In some cases it would be useful to apply other converters. One obvious example is escaping special characters ("&", "<", ">", "'" and '"') for XML and HTML. Other applications may need other converters, for example escaping special characters (like \xff) for Telnet, or escaping non-BMP characters for displaying on Tk widget, or escaping delimiters and special characters for shell, or translating to other language.
> 
> I do not think that it is practical to provide additional standard converters for all above cases, but what if create a special registry similar to registries for encodings and error handlers and allow a user to register custom converters? We could extend str.format() and f-strings to accept arbitrary letters after "!", or maybe even allow multi-character names of converters.
> 
> It is less important for f-strings because you can use arbitrary expressions, but even in this case f"Hello, {name!x}!" or f"Hello, {name!xml}!" looks better than f"Hello, {html.escape(name)}!" or f"Hello, {x(name)}!".
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/



More information about the Python-ideas mailing list