Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
    
can you be more specific.. give an example? – joel goldstick 3 hours ago
1  
I think he wants to replace the integer literal 31415926 with 31_415_926, for example. – Prune 3 hours ago
    
I'm not sure that's implemented. There are proposals, but not implementations – Andrew L. 3 hours ago
    
int('100,000'.replace(',', '')) is ok? – Nizam Mohamed 3 hours ago
1  
Guido mentioned this in this years pycon keynote youtu.be/YgtL4S7Hrwo?t=428, its coming in 3.6 – Padraic Cunningham 3 hours ago
up vote 5 down vote accepted

This is not implemented in python at the present time. You can look at the lexical analysis for strict definitions python2.7, python3.5 ...

Supposedly it will be implemented for python3.6, but it doesn't look like the documentation has been updated for that yet, nor is it available in python3.6.0a2:

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 darwin
Type "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

When it is implemented, you'll be able to use _ in your integer and float literals...

share|improve this answer

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

import locale
locale.setlocale(locale.LC_ALL, '')
locale.atoi("1,000,000")
share|improve this answer

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.

share|improve this answer

The closest thing I've seen in python is 12 * 1000 * 1000, which is not ideal, but can be useful if 12000000 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.

share|improve this answer
1  
If they are literals, python will fold the constants. It won't do any folding of symbols though. – mgilson 3 hours ago
    
Thanks. I thought it would, but I didn't want to spread bad info. – TemporalWolf 3 hours ago
2  
@ThoAppelsin -- Well, that depends on whether you want your value to be a float or an int... – mgilson 3 hours ago
3  
@TemporalWolf -- You can always check these things by disassembling the source: dis.dis(lambda : 100 * 200 * 300) – mgilson 3 hours ago
1  
@mgilson: As it happens, this is one of those things where order matters a lot. If you use x * 100 * 1000 * 1000, then it won't fold (because x * 100 might not return an int if x isn't an int). But 100 * 1000 * 1000 * x folds, because the left to right evaluation is working with known constants. – ShadowRanger 2 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.