How to use digit separators for Python integer literals? How to use digit separators for Python integer literals? python python

How to use digit separators for Python integer literals?


Update a few years later: Python 3.6 now supports PEP515, and so you can use _ for float and integer literal readability improvement.

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> 1_100011000>>>

For historical reference, you can look at the lexical analysis for strict definitions python2.7, python3.5 ...

For python3.6.0a2 and earlier, you should get an error message similar to:

Python 3.6.0a2 (v3.6.0a2:378893423552, Jun 13 2016, 14:44:21) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> 1_000  File "<stdin>", line 1    1_000        ^SyntaxError: invalid syntax>>> amount = 10_000_000.0  File "<stdin>", line 1    amount = 10_000_000.0                      ^SyntaxError: invalid syntax


Currently there is no thousands separator in Python, but you can use locale module to convert string with such separators to an int:

import localelocale.setlocale(locale.LC_ALL, '')locale.atoi("1,000,000")


There is no such function in Python but it was proposed to integrate it in the future.

You can see the proposal in the PEP515.