New submission from Guido van Rossum <gu...@python.org>:

I sometimes like to use format specs like "%-10s" % foo, which left-aligns the 
string foo in a field of 10 spaces:

>>> '%10s' % foo
'       abc'
>>> foo = 'abc'
>>> '%-10s' % foo
'abc       '

This is documented in "Format Specification Mini-Language":
https://docs.python.org/3/library/string.html#format-specification-mini-language

However it does not work with format() and f-strings:

>>> format(foo, '10s')
'abc       '
>>> format(foo, '-10s')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Sign not allowed in string format specifier
>>> f'{foo:10}'
'abc       '
>>> f'{foo:-10}'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Sign not allowed in string format specifier

In each case, without a sign it works, and right-aligns.

What am I missing?

----------
messages: 342421
nosy: gvanrossum
priority: normal
severity: normal
status: open
title: Can't left-align strings using f-strings or format()
type: behavior
versions: Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue36912>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to