• Re: Formatting a str as a number

    From Stefan Ram@3:633/280.2 to All on Mon Aug 26 01:42:24 2024
    Gilmeh Serda <gilmeh.serda@nothing.here.invalid> 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



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Mon Aug 26 01:46:25 2024
    ram@zedat.fu-berlin.de (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,}"

    .



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Wed Aug 28 07:36:47 2024
    Gilmeh Serda <gilmeh.serda@nothing.here.invalid> 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?".



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Grant Edwards@3:633/280.2 to All on Wed Aug 28 07:39:21 2024
    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


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Gilmeh Serda@3:633/280.2 to All on Fri Aug 30 15: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

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Easynews - www.easynews.com (3:633/280.2@fidonet)