This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Can't left-align strings using f-strings or format()
Type: behavior Stage: resolved
Components: Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ericvw, gvanrossum, mbussonn
Priority: normal Keywords:

Created on 2019-05-14 02:54 by gvanrossum, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg342421 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-05-14 02:54
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?
msg342423 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2019-05-14 03:03
Aren't > and < the characters to right/left-align ?

>>> foo = 'abc'
>>> f"{foo:>10}"
'       abc'
>>> format(foo, '>10')
'       abc'
msg342424 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-05-14 03:09
Aha! I thought I was missing something. :-)
msg342425 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2019-05-14 03:21
> I thought I was missing something.

Some sleep, probably.
History
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81093
2019-05-14 03:21:16mbussonnsetmessages: + msg342425
2019-05-14 03:09:01gvanrossumsetstatus: open -> closed
resolution: not a bug
messages: + msg342424

stage: resolved
2019-05-14 03:03:02mbussonnsetnosy: + mbussonn
messages: + msg342423
2019-05-14 03:01:18ericvwsetnosy: + ericvw
2019-05-14 02:54:04gvanrossumcreate