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 win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1_1000
11000
>>>
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 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
How to input numbers in python with comma separators?
Instead of commas, Python allows the use of underscores.
See https://www.python.org/dev/peps/pep-0515/
grouping decimal numbers by thousands
amount = 10_000_000.0grouping hexadecimal addresses by words
addr = 0xCAFE_F00Dgrouping bits into nibbles in a binary literal
flags = 0b_0011_1111_0100_1110same, for string conversions
flags = int('0b_1111_0000', 2)
What do underscores in a number mean?
With Python 3.6 (and PEP-515) there is a new convenience notation for big numbers introduced which allows you to divide groups of digits in the number literal so that it is easier to read them.
Examples of use:
a = 1_00_00 # you do not need to group digits by 3!
b = 0xbad_c0ffee # you can make fun with hex digit notation
c = 0b0101_01010101010_0100 # works with binary notation
f = 1_000_00.0
print(a,b,c,f)
10000
50159747054
174756
100000.0
print(int('1_000_000'))
print(int('0xbad_c0ffee', 16))
print(int('0b0101_01010101010_0100',2))
print(float('1_000_00.0'))
1000000
50159747054
174756
100000.0
A = 1__000 # SyntaxError: invalid token
Declaring a number in Python. Possible to emphasize thousand?
This is actually just now possible in Python 3.6.
You can use the first format that you showed:
a = 35_000
because underscores are now an accepted separator in numbers. (You could even say a = 3_5_00_0
, though why would you?)The second method you showed will actually create a tuple. It's the same as saying:
a = (35, 000) # Which is also the same as (35, 0).
Why type(int_int) will be int in python?
Python 3.6 introduced Underscores in Numeric Literals.
Essentially, this is a readability feature.
Compare:
x = 100000000
and:x = 100_000_000
invalid literal for int() with base 10: '2,674'
Your problem is the comma in 2,674
. Some locations use that as a decimal point, but your location does not, and you want it to separate groups of three digits in an integer. Python does not use it in that way.
So either remove the comma or change it to an underscore, _
, which recent versions of Python allow in an integer. Since I do not know your version of Python I recommend removing the comma. Use this:
number = int(like1.split()[0].replace(',', ''))
By the way, the error message you show at the top of your question is not the actual error message. The actual message shows the comma in the string:If you continue asking questions on this site, be sure to show the actual errors. In fact, you should copy and paste the entire traceback, not just the final line. That will help us to correctly understand your problem and help you.ValueError: invalid literal for int() with base 10: '2,674'
Print underscore separated integer
Try using this:
>>> x = 1_000_000
>>> print(f"{x:_}")
1_000_000
Another way would be to use format
explicitly:>>> x = 1_000_000
>>> print(format(x, '_d'))
1_000_000
Digit Separator in Julia
The standards proposal document for C++14 has a very lengthy discussion on the rationale and possible choices for a digit separator. The considered `
, '
, _
, ::
, and (space). Some of the discussion cites other languages. According to the document,
_
is also used in Ada, VHDL, Verilog, and possibly Algol68. Underscores also appear to be used in Java 7 (StackOverflow question, proposal). C++ settled on '
as their separator.
Julia hasn't formally documented underscore-separated numeric literals yet, but you can find some information in this GitHub issue (#848) and this julia-dev thread.
It doesn't look like Python has a numeric literal separator.
Related Topics
Safely Create a File If and Only If It Does Not Exist with Python
In Tensorflow, Get the Names of All the Tensors in a Graph
Why Python Has Limit for Count of File Handles
Fitting a Closed Curve to a Set of Points
Using Configparser to Read a File Without Section Name
Regex for Existence of Some Words Whose Order Doesn't Matter
How to Convert an Integer to the Shortest Url-Safe String in Python
Subclassing Python Dictionary to Override _Setitem_
What Are All the Dtypes That Pandas Recognizes
Why Does Python Use 'Magic Methods'
How to Get Tweets Older Than a Week (Using Tweepy or Other Python Libraries)
Django - Makemigrations - No Changes Detected
Counting Cars Opencv + Python Issue
Does Conda Replace the Need for Virtualenv
Brew Installation of Python 3.6.1: [Ssl: Certificate_Verify_Failed] Certificate Verify Failed