Deutsch English Français Italiano |
<mailman.26.1725237203.2917.python-list@python.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail From: dn <PythonList@DancesWithMice.info> Newsgroups: comp.lang.python Subject: Re: Formatting a str as a number - Okay, one more related thing... Date: Mon, 2 Sep 2024 12:33:16 +1200 Organization: DWM Lines: 87 Message-ID: <mailman.26.1725237203.2917.python-list@python.org> References: <oRHyO.142039$bV6e.134076@fx08.ams4> <asked-20240825164104@ram.dialup.fu-berlin.de> <combination-20240825164553@ram.dialup.fu-berlin.de> <vYqzO.8$fip4.7@fx09.ams4> <4Wtgt14jXFznWHr@mail.python.org> <mailman.12.1724794762.2917.python-list@python.org> <dGcAO.20275$Ic_6.799@fx01.ams4> <tUxAO.85679$fD72.80638@fx06.ams4> <9e13c5d9-fda3-4d7d-a87b-3dfd1c51f6ed@mrabarnett.plus.com> <df34f126-6ff8-4c0d-b4c5-e507cba6d422@DancesWithMice.info> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de Qrr8Zp3TqvRQ7Iw6bYYtMQwmLm2I84kcSBA/u4Dce2jg== Cancel-Lock: sha1:/qOMEHKizBCedEirv4ekc41MhZ0= sha256:MojdQefap3wta0fM1EI2PLP58QhdtDlzyq4cwAQL60k= Return-Path: <PythonList@DancesWithMice.info> 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=LbTSaDMF; dkim-adsp=pass; dkim-atps=neutral X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'aug': 0.07; '"""': 0.09; '=dn': 0.09; 'example:': 0.09; 'fails': 0.09; 'float': 0.09; 'from:addr:danceswithmice.info': 0.09; 'from:addr:pythonlist': 0.09; 'portugal': 0.09; 'import': 0.15; '"gnu': 0.16; "(there's": 0.16; '2024': 0.16; 'decimal': 0.16; 'integer': 0.16; 'matter?': 0.16; 'message-id:@DancesWithMice.info': 0.16; 'print(': 0.16; 'received:cloud': 0.16; 'received:rangi.cloud': 0.16; 'spain': 0.16; 'symbol': 0.16; 'ways.': 0.16; 'wrote:': 0.16; 'to:addr :python-list': 0.20; 'fri,': 0.22; 'skip:_ 10': 0.22; 'skip:p 30': 0.23; 'space': 0.26; 'else': 0.27; 'old': 0.27; '>>>': 0.28; 'example,': 0.28; 'header:User-Agent:1': 0.30; 'present': 0.30; 'code,': 0.31; 'header:Organization:1': 0.31; 'python-list': 0.32; 'but': 0.32; 'there': 0.33; 'same': 0.34; 'fill': 0.34; 'header :In-Reply-To:1': 0.34; 'words': 0.35; 'others': 0.37; 'received:192.168': 0.37; 'two': 0.39; 'on.': 0.39; 'something': 0.40; 'usa': 0.60; 'search': 0.61; 'today': 0.61; 'format': 0.62; 'public': 0.63; 'europe': 0.64; 'license': 0.64; 'your': 0.64; 'produce': 0.65; 'look': 0.65; 'skip:f 20': 0.75; 'france': 0.76; 'subject:one': 0.76; 'period': 0.81; 'country': 0.88; 'subject:more': 0.95 DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud CCBFA33BB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info; s=staff; t=1725237200; bh=Eb91miLlQHnRRgQW7VfkJiq3jT47IPxXf8NrM+9SpWY=; h=Date:From:Subject:To:References:In-Reply-To:From; b=LbTSaDMF9m3gH7BRst2f1smZM++3f8iOKEuywaQyuCKRmc3mGrrFbYeFiGP/nwnh7 QUy0nsvhQf+2KqktqaY72xbM1tpfJvXkyV0mkOFwnX875538rCqwk97T2DjG1Bgrcg wPSuMJksbFFX8USzqZf4K9HwhREC03BZw61voUJPaymSxnw7kJpr1QgzaANllIE+VM QsnpsL3kHXMm1uOlV/GK4O+8ibQfYJIlouyUuEE9/9gA/H9KXeeRgxFXNvpyPavCd1 7NVy6A/EHs3u++jUpGDukgkkmYwNBJUkfTUvq4At/J36LS+QnGysfyDmc7nkz4HJFH d/P8xrXEW1iwQ== User-Agent: Mozilla Thunderbird Content-Language: en-US In-Reply-To: <9e13c5d9-fda3-4d7d-a87b-3dfd1c51f6ed@mrabarnett.plus.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.39 Precedence: list List-Id: General discussion list for the Python programming language <python-list.python.org> List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> List-Archive: <https://mail.python.org/pipermail/python-list/> List-Post: <mailto:python-list@python.org> List-Help: <mailto:python-list-request@python.org?subject=help> List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> X-Mailman-Original-Message-ID: <df34f126-6ff8-4c0d-b4c5-e507cba6d422@DancesWithMice.info> X-Mailman-Original-References: <oRHyO.142039$bV6e.134076@fx08.ams4> <asked-20240825164104@ram.dialup.fu-berlin.de> <combination-20240825164553@ram.dialup.fu-berlin.de> <vYqzO.8$fip4.7@fx09.ams4> <4Wtgt14jXFznWHr@mail.python.org> <mailman.12.1724794762.2917.python-list@python.org> <dGcAO.20275$Ic_6.799@fx01.ams4> <tUxAO.85679$fD72.80638@fx06.ams4> <9e13c5d9-fda3-4d7d-a87b-3dfd1c51f6ed@mrabarnett.plus.com> Bytes: 7244 On 1/09/24 06:55, MRAB via Python-list wrote: > On 2024-08-31 06:31, Gilmeh Serda via Python-list wrote: >> On Fri, 30 Aug 2024 05:22:17 GMT, Gilmeh Serda wrote: >> >>> f"{int(number):>20,}" >> >> I can find "," (comma) and I can find "_" (underscore) but how about " " >> (space)? >> >> Or any other character, for that matter? >> >> Any ideas? >> >> Of course I can do f"{123456:>20_}".replace("_", " "), just thought there >> might be something else my search mojo fails on. >> > The format is described here: > > https://docs.python.org/3/library/string.html#formatspec > > A space is counted as a fill character. Rather than strict formatting, you may be venturing into "internationalisation/localisation" thinking. Different cultures/languages present numeric-amounts in their own ways. For example, a decimal point may look like a dot or period to some (there's two words for the same symbol from different English-language cultures!), whereas in Europe the symbol others call a comma is used, eg 123.45 or 123,45 - and that's just one complication of convention... For your reading pleasure, please review "locales" (https://docs.python.org/3/library/locale.html) Here's an example: Country Integer Float USA 123,456 123,456.78 France 123 456 123 456,78 Spain 123.456 123.456,78 Portugal 123456 123456,78 Poland 123 456 123 456,78 Here's some old code, filched from somewhere (above web.ref?) and updated today to produce the above:- """ PythonExperiments:locale_numbers.py Demonstrate numeric-presentations in different cultures (locales). """ __author__ = "dn, IT&T Consultant" __python__ = "3.12" __created__ = "PyCharm, 02 Jan 2021" __copyright__ = "Copyright © 2024~" __license__ = "GNU General Public License v3.0" # PSL import locale locales_to_compare = [ ( "USA", "en_US", ), ( "France", "fr_FR", ), ( "Spain", "es_ES", ), ( "Portugal", "pt_PT", ), ( "Poland", "pl_PL", ), ] print( "\n Country Integer Float" ) for country_name, locale_identifier in locales_to_compare: locale.setlocale( locale.LC_ALL, locale_identifier, ) print( F"{country_name:>10}", end=" ", ) print( locale.format_string("%10d", 123456, grouping=True, ), end="", ) print( locale.format_string("%15.2f", 123456.78, grouping=True, ) ) -- Regards =dn -- Regards, =dn