' 123,456,789's = "123456789" # arrives as str
f"{f'{int(s):,}': >20}"
Subject explains it, or ask.
| >>> format_number("123456789")
| ' 123,456,789'
Subject explains it, or ask.
This is a bloody mess:
' 123,456,789's = "123456789" # arrives as str
f"{f'{int(s):,}': >20}"
Subject explains it, or ask.
This is a bloody mess:
' 123,456,789's = "123456789" # arrives as str
f"{f'{int(s):,}': >20}"
' 123,456,789's = "123456789"
f'{int(s): >20,}'
' 123,456,789'f'{int(s):20,}'
Subject explains it, or ask.
This is a bloody mess:
' 123,456,789's = "123456789" # arrives as str
f"{f'{int(s):,}': >20}"
Subject explains it, or ask.
This is a bloody mess:
' 123,456,789's = "123456789" # arrives as str
f"{f'{int(s):,}': >20}"
' 123,456,789's = "123456789" # arrives as str
s_int = int( s ) # makes the transformation obvious and distinct
s_format = ">20," # define how the value should be presented
F"{s_int:{s_format}}"
S_FIELD_WIDTH = 20
s_format = F">{S_FIELD_WIDTH},"
RIGHT_JUSTIFIED = ">"
THOUSANDS_SEPARATOR = ","
s_format = F"{RIGHT_JUSTIFIED}{S_FIELD_WIDTH}{THOUSANDS_SEPARATOR}"
s_format = F"{S_FIELD_WIDTH}{THOUSANDS_SEPARATOR}"
and if we really want to go over-board:
RIGHT_JUSTIFIED = ">"
THOUSANDS_SEPARATOR = ","
s_format = F"{RIGHT_JUSTIFIED}{S_FIELD_WIDTH}{THOUSANDS_SEPARATOR}"
or (better) because right-justification is the default for numbers:
s_format = F"{S_FIELD_WIDTH}{THOUSANDS_SEPARATOR}"
To the extreme that if your user keeps fiddling with presentations (none
ever do, do they?), all settings to do with s_format could be added to a config/environment file, and thus be even further separated from program-logic!
and if we really want to go over-board:
RIGHT_JUSTIFIED = ">"
THOUSANDS_SEPARATOR = ","
s_format = F"{RIGHT_JUSTIFIED}{S_FIELD_WIDTH}{THOUSANDS_SEPARATOR}"
On 2024-08-26 at 20:42:32 +1200,
dn via Python-list <python-list@python.org> wrote:
and if we really want to go over-board:
RIGHT_JUSTIFIED = ">"
THOUSANDS_SEPARATOR = ","
s_format = F"{RIGHT_JUSTIFIED}{S_FIELD_WIDTH}{THOUSANDS_SEPARATOR}"
or (better) because right-justification is the default for numbers:
s_format = F"{S_FIELD_WIDTH}{THOUSANDS_SEPARATOR}"
To the extreme that if your user keeps fiddling with presentations (none
ever do, do they?), all settings to do with s_format could be added to a
config/environment file, and thus be even further separated from
program-logic!
And then you'll need a parser, many of whose Unique Challenges™ aren't
even apparent until you start parsing files from actual users, and
you'll still need some sort of fallback in the code anyway for the case
that s_format can't be parsed (for whatever reason).
Isn't a config file what just caused the global CrowdStrike outage? ;-)
That said, I understand that report generators are a thing, not to
mention RPG (https://en.wikipedia.org/wiki/IBM_RPG).
Okay, sorry; I'll just crawl back into the hole from whence I came.
f"{int(number):>20,}"
On 25 Aug 2024 15:46:25 GMT, Stefan Ram wrote:
f"{int(number):>20,}"Great. Thanks. Do you have a link to where that's documented?
I did web search, found nothing.
On 25 Aug 2024 15:46:25 GMT, Stefan Ram wrote:
f"{int(number):>20,}"
Great. Thanks. Do you have a link to where that's documented?
I did web search, found nothing.
On 2024-08-27, Gilmeh Serda via Python-list <python-list@python.org> wrote:
On 25 Aug 2024 15:46:25 GMT, Stefan Ram wrote:
f"{int(number):>20,}"
Great. Thanks. Do you have a link to where that's documented?
I did web search, found nothing.
https://docs.python.org/3/library/string.html#formatspec https://docs.python.org/3/reference/lexical_analysis.html#f-strings
For docs, I usually snag the PDFs from "python.org," especially
"reference.pdf" (the Python Language Reference) and "library.pdf"
(the Python Library Reference), then I search them for keywords.
The f-string stuff is laid out in "The Python Language Reference"[snip]
since they're part of the language itself. A quick search for
But honestly, I usually just hit up a chatbot first. I'll drop
in my code and say, "How can I make this shorter?".
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 991 |
Nodes: | 10 (1 / 9) |
Uptime: | 77:00:17 |
Calls: | 12,949 |
Calls today: | 3 |
Files: | 186,574 |
Messages: | 3,264,583 |