How do you print long integers in python?

With the line:

created_on_timestamp = str(created_on_timestamp)

You are converting something into a string. The python console represents strings with single-quotes (is this what you mean by tick marks?) The string data, of course, does not include the quotes.

When you use int() to re-convert it to a number, int() knows it's long because it's too big, and returns a long integer.

The python console represents this long number with a trailing L. but the numeric content, of course, does not include the L.

>>> l = 42000000000
>>> str(l)
'42000000000'
>>> l
42000000000L
>>> int(str(l))
42000000000L
>>> type( int(str(l)) )

Although the python console is representing numbers and strings this way (in python syntax), you should be able to use them normally. Are you anticipating a problem or have you actually run into one at this point?

In this tutorial, we will learn the different methods to print a large number in Python. And the contrast between printing integers or floats in those situations.  

As you go through the article, feel free to watch my explainer video:

How to Print All Digits of a Large Number in Python?

  • Problem Formulation: Float vs Integer Printing
    • What is the maximum possible value of an integer in Python?
    • How do you print large numbers in Python?
  • Method 1: Convert Floats to Integers
  • Method 2: Decimal Module
  • Method 3: Fractions
  • Summary

Problem Formulation: Float vs Integer Printing

Python by default prints an approximation in the case of float numbers. Most people do not need that much precision. This is to keep the number of digits manageable by displaying a rounded value instead:

>>> print(0.1)
0.1

If the numbers weren’t rounded this would be the result:

0.1000000000000000055511151231257827021181583404541015625

If you try to print the large float number, this would be the result using the print() function:

>>> print(0.1000000000000000055511151231257827021181583404541015625)
0.1

To tell you the truth, most programmers that don’t work with accounting or mathematical applications do not bother to understand why printing large floats or integer numbers is an issue in Python. They simply don’t have the need.

What is the maximum possible value of an integer in Python?

In the case of integers, those types of numbers don’t have any kind of limitation went printing large numbers. They can be as large value as memory is available.

Try running this:

y = 99999**1000
print(y)

The output should be a large integer number. And it should not have any problem displaying the results of this exponentiation.  

If you increase the exponent, let say to 10000 value, you should get the results with a message "Squeed text (255 lines).", on the IDLE Shell. 

If you double click the message, it will follow up by a warning that expands it will make it slow or unresponsive. That is because the output consumes your PC memory.  

How do you print large numbers in Python?

Is went we dwell with large float numbers, that is went problems start to arise. Example:

x=9.9999**1000
print(x)

Output:

Traceback (most recent call last):
  File "", line 1, in 
    x=9.9999**1000
OverflowError: (34, 'Result too large')

The print() function can’t show on the screen the results because the answer is too large.

The reality is that Python floats are neither of unlimited size and don’t have arbitrary precision by default, meaning: there is a size limit based on the system it is running (most common 32 or 64 bits). That restriction does not apply to integers or strings.

To know more about the technical limitations of the Python programming language read the Floating-Point Arithmetic: Issues and Limitations.

Let say the numbers you are working on are large, but not large enough to produce an overflow error. Printing the results should be easy? Right?

>>> x=9.9999**100
>>> print(x)

Output:

9.990004948383415e+99

Well, it prints a summary because the number is still too high. The print() function standard limitations can be circumvented using the following methods:

Method 1: Convert Floats to Integers

Python displays a rounded value to keep the number of digits manageable. It shows float up to 16 digits of precision. 

One method to get all the numbers is to convert the results from floats to integers using the int() function.  

>>> print(int(x))

Output:

9990004948383414538969489466669439837918658430314936559225135051476998194588052528854885488023044096

On older versions of Python, the long() function can be used to get identical results. Check PEP 237 for more information.

Method 2: Decimal Module

In cases like accounting and high-precision applications, jobs where printing the exact decimal representation is needed, use the decimal module.

Using the previous example:

>>> from decimal import Decimal
>>> Decimal.from_float(x)

Output:

Decimal('9990004948383414538969489466669439837918658430314936559225135051476998194588052528854885488023044096')

This module is designed to work in the same way as arithmetic is taught at school. It provides support for fast correctly-rounded decimal floating-point arithmetic, even for large numbers.

Method 3: Fractions

Another way to print large float numbers is supported by the fractions module which implements arithmetic based on rational numbers (so the numbers like 1/10 can be represented exactly).

>>> from fractions import Fraction
>>> Fraction(x)

Output:

Fraction(9990004948383414538969489466669439837918658430314936559225135051476998194588052528854885488023044096, 1)

Also, Python has a method that delivers the exact value of float, as a fraction: The float.as_integer_ratio()

x.as_integer_ratio()

Output:

Fraction(9990004948383414538969489466669439837918658430314936559225135051476998194588052528854885488023044096, 1)

Summary

In this tutorial, we learned three different ways to print large numbers:

  • Convert floats to integers
  • Decimal module
  • Fractions

We hope this brief article helps you understand better the ways of printing large integers and floats numbers and the limitation of each. 

How do you print a long integer in Python?

When you use int() to re-convert it to a number, int() knows it's long because it's too big, and returns a long integer. The python console represents this long number with a trailing L . but the numeric content, of course, does not include the L .

How do you print a long value in Python?

The long() function is no longer supported by Python 3. It only has one built-in integral type, named int; but it behaves mostly like the old long type. So you just need to use int() built-in function in python-3.

How does Python handle long integers?

Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate.