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: f-string decimal has leading space
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Axeinator, eric.smith, mark.dickinson
Priority: normal Keywords:

Created on 2021-04-24 18:37 by Axeinator, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
fstringdemo.py Axeinator, 2021-04-24 18:37 print statement showing the leading space behavior
Messages (2)
msg391789 - (view) Author: Akshay K (Axeinator) Date: 2021-04-24 18:37
When using a f-string to print a number with a certain number of decimal places, the number is printed with a leading space.
msg391793 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-04-24 19:14
This is by design, and documented under the "sign" section here: https://docs.python.org/3/library/string.html#format-specification-mini-language

The space before ".2f" is an instruction to leave space for a sign, "-" for a negative number and " " for a positive number. To avoid that space, use a format of ".2f" instead of " .2f".

>>> format(15, ".2f")
'15.00'
>>> format(15, " .2f")
' 15.00'
>>> format(15, "+.2f")
'+15.00'
History
Date User Action Args
2022-04-11 14:59:44adminsetgithub: 88098
2021-04-24 19:15:16mark.dickinsonsetstatus: open -> closed
resolution: not a bug
stage: resolved
2021-04-24 19:14:44mark.dickinsonsetnosy: + mark.dickinson
messages: + msg391793
2021-04-24 18:37:54Axeinatorcreate