• Re: Formatting a str as a number

    From Stefan Ram@21:1/5 to Stefan Ram on Sun Aug 25 15:46:25 2024
    [email protected] (Stefan Ram) wrote or quoted:
    | >>> format_number("123456789")
    | ' 123,456,789'

    Yeah, you totally can combine the two format specifications into one:

    f"{int(number):>20,}"

    .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Gilmeh Serda on Sun Aug 25 15:42:24 2024
    Gilmeh Serda <[email protected]d> wrote or quoted:
    Subject explains it, or ask.

    Or, you could have asked this way:

    |Please fill in the implementation (replacing "pass" below) for
    |the following function definition!
    |
    |def format_number(number_str: str) -> str:
    | """
    | Format a string of digits as a right-aligned, comma-separated number.
    |
    | This function takes a string of digits and returns a formatted string where:
    | - The number is right-aligned in a field width of 20 characters
    | - Commas are inserted as thousand separators
    | - Leading spaces are added for alignment
    |
    | Args:
    | number_str (str): A string containing only digits (0-9)
    |
    | Returns:
    | str: A formatted string with the number right-aligned and comma-separated
    |
    | Examples:
    | >>> format_number("123456789")
    | ' 123,456,789'
    | >>> format_number("1000000")
    | ' 1,000,000'
    | >>> format_number("42")
    | ' 42'
    | >>> format_number("1234567890123456789")
    | '1,234,567,890,123,456,789'
    | """
    | pass

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gilmeh Serda@21:1/5 to Stefan Ram on Tue Aug 27 20:48:27 2024
    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.

    --
    Gilmeh

    When a fellow says, "It ain't the money but the principle of the thing,"
    it's the money. -- Kim Hubbard

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Gilmeh Serda on Tue Aug 27 21:36:47 2024
    Gilmeh Serda <[email protected]d> wrote or quoted:
    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.

    Stoked to hear you're into it!

    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"
    since they're part of the language itself. A quick search for
    "f-str" gets you to "2.4.3 Formatted string literals," which says,
    "The result is then formatted using the format() protocol." So, you
    got to check out "The Python Library Reference" too! Searching for
    "format(" there zooms you right to "6.1.3 Format String Syntax"
    (via "Format Specification Mini-Language").

    But honestly, I usually just hit up a chatbot first. I'll drop
    in my code and say, "How can I make this shorter?".

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gilmeh Serda@21:1/5 to Grant Edwards on Fri Aug 30 05:22:17 2024
    On Tue, 27 Aug 2024 17:39:21 -0400 (EDT), Grant Edwards wrote:

    On 2024-08-27, Gilmeh Serda via Python-list <[email protected]> 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

    Thanks. I'll check those as time allows.

    --
    Gilmeh

    National security is in your hands - guard it well.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gilmeh Serda@21:1/5 to Stefan Ram on Fri Aug 30 05:25:40 2024
    On 27 Aug 2024 21:36:47 GMT, Stefan Ram wrote:

    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.

    Thanks, that's a good idea. I've usually gone for the web docs only.

    The f-string stuff is laid out in "The Python Language Reference"
    since they're part of the language itself. A quick search for
    [snip]

    Great. Thanks. I'll try to remember that.

    But honestly, I usually just hit up a chatbot first. I'll drop
    in my code and say, "How can I make this shorter?".

    In other words, a shortcut. :)

    --
    Gilmeh

    More people are flattered into virtue than bullied out of vice. -- R. S. Surtees

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)