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
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to