Path: ...!news.mixmin.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: dn Newsgroups: comp.lang.python Subject: Re: Is there a better way? [combining f-string, thousands separator, right align] Date: Mon, 26 Aug 2024 20:42:32 +1200 Organization: DWM Lines: 53 Message-ID: References: <09102d57-41cd-4428-b96f-d69e2ffe9c95@DancesWithMice.info> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de liRU+UsTQJDI5fHU4HUkLQp1Vh9GsgHlMkeRfqGkMtXg== Cancel-Lock: sha1:CH5cfXM74ZLSpWf94AobkPPpa1k= sha256:YIg6dh6buYHp4KCClPZt31lWOe9NWxv+skw85FL32Oo= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org Authentication-Results: mail.python.org; dkim=pass reason="2048-bit key; unprotected key" header.d=danceswithmice.info header.i=@danceswithmice.info header.b=PJTVBSBV; dkim-adsp=pass; dkim-atps=neutral X-Spam-Status: OK 0.068 X-Spam-Evidence: '*H*': 0.86; '*S*': 0.00; 'string': 0.07; '=dn': 0.09; 'from:addr:danceswithmice.info': 0.09; 'from:addr:pythonlist': 0.09; 'subject:, \n ': 0.09; '>>>>': 0.16; 'css': 0.16; 'expressions': 0.16; 'indeed': 0.16; 'message- id:@DancesWithMice.info': 0.16; 'received:cloud': 0.16; 'received:rangi.cloud': 0.16; 'subject:align': 0.16; 'subject:string': 0.16; 'subject:way': 0.16; 'wrote:': 0.16; 'to:addr:python-list': 0.20; 'do,': 0.26; '>>>': 0.28; 'settings': 0.28; 'it,': 0.29; 'header:User-Agent:1': 0.30; 'default': 0.31; 'header:Organization:1': 0.31; 'ease': 0.32; 'python-list': 0.32; 'subject:there': 0.32; 'received:192.168.1': 0.32; 'header:In- Reply-To:1': 0.34; 'presented': 0.37; "skip:' 10": 0.37; 'really': 0.37; 'received:192.168': 0.37; 'could': 0.38; 'added': 0.39; 'define': 0.40; 'both': 0.40; 'want': 0.40; 'should': 0.40; 'higher': 0.60; 'format': 0.62; 'subject': 0.63; 'ever': 0.63; 'thus': 0.64; 'your': 0.64; 'matter': 0.68; 'further': 0.69; 'obvious': 0.69; 'within': 0.69; 'subject:]': 0.70; 'skip:f 30': 0.71; 'skip:f 20': 0.75; 'html': 0.80; 'states': 0.80; 'further,': 0.84; 'subject:better': 0.91 DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud C50C56844 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info; s=staff; t=1724661758; bh=59vOgzeQKyopLDGSvbbA97ul7OUQ9s4daPAhx8zY92U=; h=Date:From:Subject:To:References:In-Reply-To:From; b=PJTVBSBVdEReMjd2tTnLZvRGQD8ge3e+ILR9c9YdT1gi5OTuW3CnXvro+qAB1n+7C mf4M9Ii+H0uv8IB/IzD0OY3ygb5A+62O27pcy/S0Bi7vd8g19pmzca0pzFxFgdOVik OoIkNuXG3JN26C6ZBKzyUmimm/GDcTLbzn8K0KCnUdCKL6NoCDB2/4JyN/GYEgFz6P DDPzyLJu+dyGxmGUsTr9veoBolZn7MijTW6HVx44xj8SiIS0zwmpDeQVzsqMpnOpsE j7EeoIU/7BDGXhzq5MBG60gEXtA62C8CYvEtsq118SEy+SjyqsbM5se1ffGuAvSvXz Qze0axJ+iypsA== User-Agent: Mozilla Thunderbird Content-Language: en-US In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.39 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <09102d57-41cd-4428-b96f-d69e2ffe9c95@DancesWithMice.info> X-Mailman-Original-References: Bytes: 5531 On 26/08/24 03:12, Gilmeh Serda via Python-list wrote: > Subject explains it, or ask. > > This is a bloody mess: > >>>> s = "123456789" # arrives as str >>>> f"{f'{int(s):,}': >20}" > ' 123,456,789' With recent improvements to the expressions within F-strings, we can separate the string from the format required. (reminiscent of FORTRAN which had both WRITE and FORMAT statements, or for that matter HTML which states the 'what' and CSS the 'how') Given that the int() instance-creation has a higher likelihood of data-error, it is recommended that it be a separate operation for ease of fault-finding - indeed some will want to wrap it with try...except. >>> 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}}" ' 123,456,789' Further, some of us don't like 'magic-constants', hence (previously): >>> S_FIELD_WIDTH = 20 >>> s_format = F">{S_FIELD_WIDTH}," 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! -- Regards, =dn