[issue44146] Format string fill not handling brace char

Eric V. Smith report at bugs.python.org
Sun May 16 19:17:23 EDT 2021


Eric V. Smith <eric at trueblade.com> added the comment:

In the future, if you get an error, please tell us what the error is. It makes responding to bugs easier.

'{::>10d'.format(5)
Gives me an error with 3.10:

>>> '{::>10d'.format(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: unmatched '{' in format spec

And your second example:

>>> '{:{>10d'.format(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: unmatched '{' in format spec

This root cause of treating left braces as special is because braces can nest. I don't think there's any way of doing what you want, just like you can't use a closing brace as part of a format spec while using str.format() or f-strings.

width=10
>>> f'{5:{width}}'
'         5'
>>> '{0:{1}}'.format(5, width)
'         5'


If you really want to use braces in the format spec, you should use format, which does not parse braces:

>>> format(5, '{>10d')
'{{{{{{{{{5'

----------
nosy: +eric.smith

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


More information about the Python-bugs-list mailing list