# Add underscore as a thousandths separator for string formatting

**URL:** <https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407>\
**Category:** Ideas\
**Created:** [February 26, 2021, 9:10pm UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407 "2021-02-26T21:10:11Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![terrdavis](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/terrdavis/32/1068_2.png) [@terrdavis](https://discuss.python.org/u/terrdavis)\
**Post date:** [February 26, 2021, 9:10pm UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/1 "2021-02-26T21:10:12Z")

</div>

I can think of two different ways to do this:

1. Special case the `_` `grouping_option` to add an underscore in the thousandths places
2. Add a new format specifier (like `float_grouping`):

```nohighlight
format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.precision[float_grouping]][type]
fill ::= <any character>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= digit+
grouping_option ::= "_" | ","
float_grouping ::= "_"
precision ::= digit+
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"

```

Let me know if there’s been discussion of this already, I haven’t looked.

---

<div class="post-metadata">

**Author:** ![methane](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/methane/32/43_2.png) [@methane](https://discuss.python.org/u/methane)\
**Post date:** [February 27, 2021, 12:59am UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/2 "2021-02-27T00:59:33Z")

</div>

I don’t understand what you are proposing.

```auto
>>> i=10**10
>>> f"{i:_d}"
'10_000_000_000'

```

Why do we need another way?

---

<div class="post-metadata">

**Author:** ![terrdavis](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/terrdavis/32/1068_2.png) [@terrdavis](https://discuss.python.org/u/terrdavis)\
**Post date:** [February 27, 2021, 1:50am UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/3 "2021-02-27T01:50:53Z")

</div>

Current: `f”{4321.23456:_.6f}”` → `4_321.123456`

Proposed:

`f”{4321.23456:_.6f}”` → `4_321.123_456`

Or

`f”{4321.23456:_.6f}”` → `4321.123_456`  
`f”{4321.23456:_._6f}”` → `4_321.123_456`

## Edit: above two should be:

`f”{4321.23456:_.6f}”` → `4_321.123456`  
`f”{4321.23456:._6f}”` → `4321.123_456`

## /edit

This is a different spec than the original post:

`format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.[float_grouping][precision]][type]`

---

<div class="post-metadata">

**Author:** ![EpicWink](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/epicwink/32/17968_2.png) [@EpicWink](https://discuss.python.org/u/EpicWink)\
**Post date:** [February 27, 2021, 11:01am UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/4 "2021-02-27T11:01:55Z")

</div>

> [@methane](#):
>
> I don’t understand what you are proposing.

Ah, funny misread of the title 😃

“Thousandths”, not “thousands”

---

<div class="post-metadata">

**Author:** ![domdfcoding](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/domdfcoding/32/2544_2.png) [@domdfcoding](https://discuss.python.org/u/domdfcoding)\
**Post date:** [February 27, 2021, 5:10pm UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/5 "2021-02-27T17:10:41Z")

</div>

I think `f”{4321.23456:_._6f}”` is better than `f”{4321.23456:_.6f}”` as it preserves backwards compatibility and makes it obvious the separators comes both after and before the decimal place.

You could then also do

`f”{4321.23456:._6f}”` → `4321.123_456`

---

<div class="post-metadata">

**Author:** ![guido](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guido/32/21_2.png) [@guido](https://discuss.python.org/u/guido)\
**Post date:** [February 27, 2021, 10:47pm UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/6 "2021-02-27T22:47:01Z")

</div>

I’d be curious to understand why this doesn’t already use `_` in the fractional part. An oversight? Intentional? Left for future generations to solve?

---

<div class="post-metadata">

**Author:** ![ericvsmith](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@ericvsmith](https://discuss.python.org/u/ericvsmith)\
**Post date:** [February 28, 2021, 7:49am UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/7 "2021-02-28T07:49:15Z")

</div>

I think we were focused on ints and only later applied it to floats, and the idea of also formatting the fractional part just never came up.

---

<div class="post-metadata">

**Author:** ![daniele](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@daniele](https://discuss.python.org/u/daniele)\
**Post date:** [February 28, 2021, 2:54pm UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/8 "2021-02-28T14:54:32Z")

</div>

AFAIK, typographical conventions in most languages do not apply the thousands separator to the fractional part.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [March 1, 2021, 5:54pm UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/9 "2021-03-01T17:54:33Z")

</div>

It is true that most human languages do not apply a thousands separator  
to the fractional part, but they do often apply a narrow space instead.  
I can’t show narrow spaces in this email, so I’ll use a regular space.  
Pretend it is half the width of a digit, or even less:

```
12,345,678.000 123 456 789

```

Many European countries swap the decimal point and the comma; India, I  
believe, groups the digits in fours rather than threes.

In any case, practicality beats purity: there is no typographical  
convention to use underscores in any human language, as far as I know,  
nevertheless it is a useful thing to do when programming. We support  
underscores in numeric literals:

```
>>> 123_456.123_456
123456.123456

```

and we can output underscores in the integer part of the number. We  
should support them in the fractional part as well.

---

<div class="post-metadata">

**Author:** ![terrdavis](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/terrdavis/32/1068_2.png) [@terrdavis](https://discuss.python.org/u/terrdavis)\
**Post date:** [March 1, 2021, 7:26pm UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/10 "2021-03-01T19:26:25Z")

</div>

It sounds like the backwards-compatible version would be preferable:

`f”{4321.23456:_.6f}”` → `4_321.123456`  
`f”{4321.23456:._6f}”` → `4321.123_456`  
`f”{4321.23456:_._6f}”` → `4_321.123_456`

Alternative names for “float\_grouping”:

- fractional\_grouping
- fractional\_grouping\_option
- ??

```nohighlight
format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.[float_grouping][precision]][type]
fill ::= <any character>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= digit+
grouping_option ::= "_" | ","
float_grouping ::= "_"
precision ::= digit+
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"

```

Is there a way to say that the the dot **must** be followed by one or both of float\_grouping and precision?  
I believe this grammar specification would allow this `f"{123:0.f}"` which is invalid and meaningless.

Also, since we can format numbers in a non-round-trippable way, why not add an option to group the fractional part with a special space (something like `float_grouping ::= "_" | "s"`):

```auto
print(".000\N{NO-BREAK SPACE}345")           
print(".000\N{MEDIUM MATHEMATICAL SPACE}345")
print(".000\N{THIN SPACE}345")               
print(".000\N{HAIR SPACE}345")               

```

Here’s an image, since the spaces are replaced by disqus:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/0/0af2c4a12bfd1ad086122d341514d0d57209d040.png)  
edit: you can see the difference better using ones:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/4/4f43e552f1de49325db24237cb45a8c76a1adeda.png)

---

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [March 2, 2021, 7:19am UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/11 "2021-03-02T07:19:43Z")

</div>

Since all this is language specific (What are fraction and group separators? What is the size of group?) should not it be in the locale module instead of general str method or builtin?

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [March 2, 2021, 8:04am UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/12 "2021-03-02T08:04:13Z")

</div>

That boat sailed over the horizon a long time ago: we’ve accepted  
underscores in numeric literals, and as output in the whole number part,  
for a long time now.

```
>>> format(12_34_56.12_34_56, '_f')
'123_456.123456'

```

works back to at least 3.7 and maybe older.

We’re not suggesting a fully configurable formatting language capable of  
specifying the group size and seperators, just allowing output of  
underscores in the fraction part.

```
>>> format(12_34_56.12_34_56, '_._f') # Proposal.
'123_456.123_456'

```

The group size will remain fixed at three; the grouping seperator will  
be fixed as either underscore or comma.

Anything more complicated than that probably belongs in locale, or maybe  
the string module as a float formatter class. But that’s not this  
proposal.

---

<div class="post-metadata">

**Author:** ![terrdavis](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/terrdavis/32/1068_2.png) [@terrdavis](https://discuss.python.org/u/terrdavis)\
**Post date:** [March 5, 2021, 9:00pm UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/13 "2021-03-05T21:00:05Z")

</div>

Any chance of this making it into 3.10? The last alpha is a month away, and the first beta is a month after that.

> **[PEP 619 -- Python 3.10 Release Schedule](https://www.python.org/dev/peps/pep-0619/#id5)**
>
> The official home of the Python Programming Language

I’ve never contributed to CPython, and I have minimal experience with C. Could this be a reasonable first contribution?

---

<div class="post-metadata">

**Author:** ![terrdavis](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/terrdavis/32/1068_2.png) [@terrdavis](https://discuss.python.org/u/terrdavis)\
**Post date:** [March 25, 2021, 5:57pm UTC](https://discuss.python.org/t/add-underscore-as-a-thousandths-separator-for-string-formatting/7407/14 "2021-03-25T17:57:06Z")

</div>

bpo issue here:  
[https://bugs.python.org/issue43624](https://bugs.python.org/issue43624)
