[New-bugs-announce] [issue44355] Allow spaces in format strings

Steven D'Aprano report at bugs.python.org
Tue Jun 8 21:10:05 EDT 2021


New submission from Steven D'Aprano <steve+python at pearwood.info>:

Format strings should allow spaces around keys and indices. This might be as simple as running str.strip() on the contents of curly braces?

Aside from indentation and newlines, in most other contexts whitespace is insignificant. E.g. in subscripting `seq[ index ]`. But format strings treat spaces as part of the index or key, which is surprising.

f-strings, on the other hand, already allow spaces around expressions and keys:

    >>> name = 'Brian'
    >>> f'{ name.upper() }'
    'BRIAN'


Examples:

    '{ }'.format(30)
    Expect to get '30'
    but get KeyError: ' '


    '{ 0 }'.format(30)
    Expect to get '30'
    but get KeyError: ' 0 '

    '{ x }'.format(x=30)
    Expect to get '30'
    but get KeyError: ' x '


See discussion here:

https://discuss.python.org/t/please-help-key-error/9168/1

----------
messages: 395371
nosy: steven.daprano
priority: normal
severity: normal
status: open
title: Allow spaces in format strings
type: enhancement
versions: Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44355>
_______________________________________


More information about the New-bugs-announce mailing list