Does the print function in python have a limit of string length it can print?

11,418

Solution 1

while this doesn't answer your question, for moving loads of data print is probably a bad idea: print is meant for short informational printouts. it provides features you usually don't want when moving large data, like formatting and appending an EOL.

Instead use something more low-level like write on the sys.stdout filehandle (or some other filehandle; this allows you to easily write to a file instead of stdout)

 out=sys.stdout
 out.write(largedata)

you also probably want to re-chunk the data before outputting:

 # this is just pseudocode:
 for chunk in largedata:
     out.write(chunk)

.write does not append an EOL character, so the output of multiple chunks will be virtually indistinguishable from outputting all in one big go. but you have the benefit of not overrunning any buffers.

Solution 2

About the maximum size of your string which you can print in stdout using print function, since you are have to pass your text as a python object to print function and since the max size of your variable is depend on your platform it could be 231 - 1 on a 32-bit platform and 263 - 1 on a 64-bit platform.

Also you can use sys.maxsize to get the max size of your variables :

sys.maxsize

An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2**31 - 1 on a 32-bit platform and 2**63 - 1 on a 64-bit platform.

Share:
11,418

Related videos on Youtube

shrinidhisondur
Author by

shrinidhisondur

Data Structures, Algorithms, Learning C ++, Java

Updated on September 16, 2022

Comments

  • shrinidhisondur
    shrinidhisondur over 1 year

    I am trying to print a large string, and it is in the magnitudes of a 100 Mb, and it needs to be done in one shot. Looks like it is getting truncated.

    • justinpawela
      justinpawela almost 9 years
      You are printing over 100 Mb to stdout? To a terminal?
    • umläute
      umläute almost 9 years
      what do you try to achieve by doing it in "one shot"?
    • Tanmaya Meher
      Tanmaya Meher almost 9 years
      why buddy why ??? Is it necessary to unnecessarily populate the terminal or you are just curious to put things to the edge?! :P