4

I know for example "{:,}".format(123456789) will print '123,456,789' as output in python.

However, instead of the commas I would like the thin space character as the thousands separator. According to here, the python source encoding for the thin space character is u"\u2009".

So I tried replacing the comma in "{:,}" by u"\u2009", but that results in a syntax error. This might be because python can't deal with the resulting nested double quotes and something needs to be escaped.

So is there a way to make this, or some other mechanism work? And by the way I'd like to suppress the single quotes that appear in the output as well.

cs95
  • 379,657
  • 97
  • 704
  • 746
redwood_gm
  • 343
  • 2
  • 11
  • It would help to show an example and the exact error. `"{:\u2009}".format(123456789)` gives a different error: `ValueError: Unknown format code '\x2009' for object of type 'int'` but at least its not a syntax error! – tdelaney Jun 04 '18 at 06:12
  • As per https://docs.python.org/3/library/string.html#format-specification-mini-language you can have only `_` or `,` as grouping option. So, you should use string replace as mentioned in the answers below. – skjoshi Jun 04 '18 at 06:40

2 Answers2

6

You can use replace method from str to replace , by thin spaces after having formatted it, i.e: "{:,}".format(123456789).replace(",", u"\u2009")

According to the official documentation here, only , and _ can be used as separators. To use a user-defined one, you need to use the n representation, and change your locale settings (see https://docs.python.org/3/library/locale.html#locale.LC_NUMERIC). You can create a new localization and use this fake localization for what you want but this is a rather complicated solution.

EDIT: By a fake localization, I really mean a new locale in your computer, not in Python. You can read https://askubuntu.com/questions/653008/how-to-create-a-new-system-locale for more details (I think this can be adapterd on most distributions)

picnix_
  • 377
  • 2
  • 7
3

I can offer you an obvious workaround:

>>> n = 123456789
>>> print(f'{n:,}'.replace(',', u'\u2009'))
123 456 789

Works for str.format the same way.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • 1
    I tried you solution a long time ago in both python and ipython, but in both cases the thousands separator turned out to be the space rather than the thin space. I'm puzzled as to how you got a thin space, since on my Mac's python at least, the thin space is rendered as a space. – redwood_gm May 18 '19 at 00:23
  • 1
    @redwood_gm Beware that Jupyter notebooks uses a monospaced font by default, in which there is no visual distinction between thin and standard spaces. That does not mean the workaround didn’t work (you can obviously check by assigning to a variable instead of printing). – Skippy le Grand Gourou Jan 25 '23 at 14:02
  • What Skippy said also applies to most terminal emulators, since they rely on fixed width (monospaced) fonts to display properly. Try changing to a non-monospaced font and see how happy you are with it (you will be very unhappy with it). – Z4-tier Mar 31 '23 at 00:39