[New-bugs-announce] [issue41250] Number separators in different places

wyz23x2 report at bugs.python.org
Wed Jul 8 21:30:21 EDT 2020


New submission from wyz23x2 <wyz23x2 at 163.com>:

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'

----------
components: Interpreter Core, Library (Lib)
messages: 373367
nosy: wyz23x2
priority: normal
severity: normal
status: open
title: Number separators in different places
versions: Python 3.10, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41250>
_______________________________________


More information about the New-bugs-announce mailing list