Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 995
shrinidhisondur Avatar asked Jul 14 '15 07:07

shrinidhisondur


People also ask

What is the max length of string in Python?

On a 32 bit system it's around 2 billion or 500 million characters.

How do you print the length of a string in Python?

To get the length of a string, use the len() function.

How do you print a string 20 times in Python?

Use range() to print a string multiple times Use range(stop) to create a range of 0 to stop where stop is the number of lines desired. Use a for-loop to iterate through this range. In each iteration, concatenate the string to itself by the number of times desired and print the result.

Do strings have length Python?

To calculate the length of a string in Python, you can use the built-in len() method. It takes a string as a parameter and returns an integer as the length of that string. For example len(“educative”) will return 9 because there are 9 characters in “educative”.


2 Answers

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.

like image 85
umläute Avatar answered Oct 30 '22 13:10

umläute


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.

like image 43
Mazdak Avatar answered Oct 30 '22 14:10

Mazdak