Is there any way to group digits in a Python code to increase code legibility? I've tried '
and _
which are digit separators of some other languages, but no avail.
A weird operator which concatenates its left hand side with its right hand side could also work out.
Update a few years later: Python 3.6 now supports PEP515, and so you can use _ for float and integer literal readability improvement.
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:
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.
The closest thing I've seen in python is
12 * 1000 * 1000
, which is not ideal, but can be useful if12000000
is needed. Be advised though, while in C, those are equivalent, because at compile time it converts both to the same thing, python may not share this optimization.Currently there is no thousands separator in Python, but you can use
locale
module to convert string with such separators to an int: