classification
Title: Number separators in different places
Type: Stage: resolved
Components: Interpreter Core, Library (Lib) Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, rhettinger, wyz23x2
Priority: normal Keywords:

Created on 2020-07-09 01:30 by wyz23x2, last changed 2020-07-09 18:05 by rhettinger. This issue is now closed.

Messages (5)
msg373367 - (view) Author: wyz23x2 (wyz23x2) * Date: 2020-07-09 01:30
The current syntax is this for thousand separators:
f'{var:,}'
It will return this when var is 1234567:
'1,234,567'
But sometimes we need a way to insert them in other places. For example:
123456789 → '1,2345,6789' (4)
62938757312 → '6,29387,57312' (5)
This could be done like this:
Idea 1:
Add a new method to string:
string.sep(num: int_or_float, interval: int_or_iterable = 3, 
           sepchar: str = ',')
>>> import string
>>> string.sep(1234567, 3)
'1,234,567'
>>> string.sep(1234567890, range(1, 4))
'1,23,456,7890'
>>> string.sep('Hello')
TypeError: Invalid number 'Hello'
>>> string.sep(12345678, sepchar=' ')
'12 345 678'
>>> string.sep(123456789, 4, '|')
'1|2345|6789'

Idea 2: (Not as powerful as above)
(Future)
>>> f'{123456789:4,}'
'1,2345,6789'
>>> f'{62938757312:5,}'
'6,29387,57312'
>>> f'{1234567:,}' # Equal to f'{1234567:3,}'
'1,234,567'
(Current)
>>> f'{12345678:5,}' # 5 discarded
'12,345,678'
msg373368 - (view) Author: wyz23x2 (wyz23x2) * Date: 2020-07-09 01:45
Q: Why not use f"{var:,}".replace(',', sepchar) for the sepchar parameter?
A: It is very complicated in the case below:
num = 1234567
text = 'Hello, world!'
print(f"{num:,}{text}").replace(',', ' ') # Becomes '1 234 567Hello world!'
print(f"{f'{num:,}'.replace(',', ' ')}{text}") # Too complicated!
print(f"{num:,}".replace(',', ' ')+text) # Slow!
msg373374 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-07-09 04:55
This was considered in PEP 378 — Format Specifier for Thousands Separator.¹  The decision was to keep it simple and only support groups of three digits using a comma as the separator.

FWIW, the decimal documentation has a formatting recipe that could be adapted to meet your needs.²

¹ https://www.python.org/dev/peps/pep-0378/
² https://docs.python.org/3/library/decimal.html#recipes
msg373379 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-07-09 06:29
The formatting specification language is already complicated enough without adding even more to it. As PEP 378 says "It is not the goal to replace the locale module, to perform internationalization tasks, or accommodate every possible convention."

Your best bet is to write your own formater and use that:

f'{formater(v)}'

You could also pass it whatever parameters you want, like specifying the number of digits or the separator or whether to use the locale to determine these things.

The PEP suggests Babel (http://babel.pocoo.org/en/latest/), which I don't have any experience with, but seems pretty complete.
msg373413 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-07-09 18:05
Thank you for the suggestion, but we'll decline for the reasons listed in the PEP and those listed by Eric.
History
Date User Action Args
2020-07-09 18:05:47rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg373413

stage: resolved
2020-07-09 06:29:06eric.smithsetmessages: + msg373379
2020-07-09 04:55:17rhettingersetnosy: + rhettinger
messages: + msg373374
2020-07-09 03:58:45xtreaksetnosy: + eric.smith
2020-07-09 01:45:21wyz23x2setmessages: + msg373368
2020-07-09 01:30:21wyz23x2create