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.
On a 32 bit system it's around 2 billion or 500 million characters.
To get the length of a string, use the len() function.
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.
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”.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With