# Can Python have support for number separators?

**URL:** <https://discuss.python.org/t/can-python-have-support-for-number-separators/46730>\
**Category:** Python Help\
**Created:** [February 23, 2024, 8:15pm UTC](https://discuss.python.org/t/can-python-have-support-for-number-separators/46730 "2024-02-23T20:15:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daveson217](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/daveson217/32/18113_2.png) [@Daveson217](https://discuss.python.org/u/Daveson217)\
**Post date:** [February 23, 2024, 8:15pm UTC](https://discuss.python.org/t/can-python-have-support-for-number-separators/46730/1 "2024-02-23T20:15:49Z")

</div>

I see that when I call something like int(2,345,565), it gives an error. Interestingly, in Java, this is supported. Is it a good idea to have something like this in Python?

---

<div class="post-metadata">

**Author:** ![Jelle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jelle/32/1049_2.png) [@Jelle](https://discuss.python.org/u/Jelle)\
**Post date:** [February 23, 2024, 8:17pm UTC](https://discuss.python.org/t/can-python-have-support-for-number-separators/46730/2 "2024-02-23T20:17:53Z")

</div>

You’re a few years too late:

```python
% python
Python 3.11.1 (main, Dec 21 2022, 16:19:04) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2_345_678
2345678
>>> 

```

Added in Python 3.6 by PEP 515:

> **[PEP 515 – Underscores in Numeric Literals | peps.python.org](https://peps.python.org/pep-0515/)**
>
> Python Enhancement Proposals (PEPs)

---

<div class="post-metadata">

**Author:** ![Daveson217](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/daveson217/32/18113_2.png) [@Daveson217](https://discuss.python.org/u/Daveson217)\
**Post date:** [February 23, 2024, 8:58pm UTC](https://discuss.python.org/t/can-python-have-support-for-number-separators/46730/3 "2024-02-23T20:58:11Z")

</div>

Ouch 😂😂😂😂😁😁. I never knew this. I was revisiting Java and the thought crossed my mind. It’s good that I am now getting ideas to contribute to open source though. Lol

Thanks a bunch

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [February 23, 2024, 9:37pm UTC](https://discuss.python.org/t/can-python-have-support-for-number-separators/46730/4 "2024-02-23T21:37:57Z")

</div>

How does the Java code look like? I was curious and googled it but can’t find it.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [February 23, 2024, 10:47pm UTC](https://discuss.python.org/t/can-python-have-support-for-number-separators/46730/5 "2024-02-23T22:47:35Z")

</div>

I can’t find any evidence that Java supports any kind of thousands separator in the _literal_ syntax for integers. Of course it’s more than capable of _parsing a string_ which contains thousands separators to create an integer (or floating-point) value.

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [February 24, 2024, 2:20am UTC](https://discuss.python.org/t/can-python-have-support-for-number-separators/46730/6 "2024-02-24T02:20:42Z")

</div>

Excuse me for chiming in. But since @Daveson217 specifically asked for a more natural number conversion to `int` using thousands comma separators (in the west and period for European based system), is there a number conversion `built-in to int` not using underscores whose syntax is not generally `natural` ?

\*\*\* Update \*\*\*

I suppose you can do your own conversion manually assuming you enter the number as a string, however:

```python
from re import sub

def str_to_int(num):
    
    remv_comma = sub(',', '', num)
    return int(remv_comma)

# test function    
add_nums = 123123 + str_to_int('4,123,123')
print(add_nums)

```

---

<div class="post-metadata">

**Author:** ![Daveson217](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/daveson217/32/18113_2.png) [@Daveson217](https://discuss.python.org/u/Daveson217)\
**Post date:** [February 24, 2024, 6:24am UTC](https://discuss.python.org/t/can-python-have-support-for-number-separators/46730/7 "2024-02-24T06:24:23Z")

</div>

You are right @kknechtel.  
However, the code I speak of is in the **Java source code** for processing input, `Scanner`; Python’s equivalent of the `input` function.

See below:

```python
import java.util.Scanner;

public class AskAge {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your age");
        int age = sc.nextInt();
    }
}

```

Then somewhere down the rabbit hole of calling various methods (starting from the sc.nextInt(), the following method is called:

```python
private String processIntegerToken(String token) {
        String result = token.replaceAll(""+groupSeparator, "");
        boolean isNegative = false;
        int preLen = negativePrefix.length();
        if ((preLen > 0) && result.startsWith(negativePrefix)) {
            isNegative = true;
            result = result.substring(preLen);
        }
        int sufLen = negativeSuffix.length();
        if ((sufLen > 0) && result.endsWith(negativeSuffix)) {
            isNegative = true;
            result = result.substring(result.length() - sufLen,
                                      result.length());
        }
        if (isNegative)
            result = "-" + result;
        return result;
    }

```

From the docs, it says:

> The integer token must be stripped of prefixes, group separators, and suffixes, non ascii digits must be converted into ascii digits before parse will accept it.

cc: @Stefan2

---

<div class="post-metadata">

**Author:** ![Daveson217](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/daveson217/32/18113_2.png) [@Daveson217](https://discuss.python.org/u/Daveson217)\
**Post date:** [February 24, 2024, 6:26am UTC](https://discuss.python.org/t/can-python-have-support-for-number-separators/46730/8 "2024-02-24T06:26:43Z")

</div>

This works perfectly as well. @onePythonUser

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [February 24, 2024, 6:46am UTC](https://discuss.python.org/t/can-python-have-support-for-number-separators/46730/9 "2024-02-24T06:46:43Z")

</div>

> [@Daveson217](#):
>
> However, the code I speak of is in the **Java source code** for processing input, `Scanner`; Python’s equivalent of the `input` function.

Not really.

`input` in Python gets a string, no matter what is typed. It’s the equivalent of the `readline` method on a `DataInputStream`. You need to use other code to interpret the contents of the string to get other values of other types. `java.util.Scanner` is a completely different interface. You can make something work like that in Python, but it isn’t built-in and isn’t the normal way to do things in Python. Especially not when reading input from the user - because of **how the terminal works** , it will naturally be much simpler and less error-prone to read the input a line at a time consistently and do separate parsing on each line.

If you want to parse a string that is expected to represent a number that uses a thousands separator, see:

> <https://stackoverflow.com/questions/6633523/how-can-i-convert-a-string-with-dot-and-comma-into-a-float-in-python>

> <https://stackoverflow.com/questions/1779288/how-to-convert-a-string-to-a-number-if-it-has-commas-in-it-as-thousands-separato>

---

<div class="post-metadata">

**Author:** ![TeamSpen210](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/teamspen210/32/1004_2.png) [@TeamSpen210](https://discuss.python.org/u/TeamSpen210)\
**Post date:** [February 24, 2024, 7:03am UTC](https://discuss.python.org/t/can-python-have-support-for-number-separators/46730/10 "2024-02-24T07:03:44Z")

</div>

In addition to the `locale` module, you may want to look into the [`babel`](https://babel.pocoo.org/en/latest/index.html) package, which provides more advanced facilities for handling specific languages and cultures.

---

<div class="post-metadata">

**Author:** ![Daveson217](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/daveson217/32/18113_2.png) [@Daveson217](https://discuss.python.org/u/Daveson217)\
**Post date:** [February 24, 2024, 7:23am UTC](https://discuss.python.org/t/can-python-have-support-for-number-separators/46730/11 "2024-02-24T07:23:03Z")

</div>

Thanks for letting me know @kknechtel.

I have not used the locale package before.
