Issue 36912: Can't left-align strings using f-strings or format()
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) * ![]() |
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) * ![]() |
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:15 | admin | set | github: 81093 |
| 2019-05-14 03:21:16 | mbussonn | set | messages: + msg342425 |
| 2019-05-14 03:09:01 | gvanrossum | set | status: open -> closed resolution: not a bug messages: + msg342424 stage: resolved |
| 2019-05-14 03:03:02 | mbussonn | set | nosy:
+ mbussonn messages: + msg342423 |
| 2019-05-14 03:01:18 | ericvw | set | nosy:
+ ericvw |
| 2019-05-14 02:54:04 | gvanrossum | create | |
