Python Forum

Full Version: Print Function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
HI Friends,

I want to know how "PRINT()" function is working in back end.
Mouse over VS Code.
[Image: 1SRz4P.jpg]
See parameter that print() function has.
>>> n = 9
>>> print("n=", n, sep='00000', end='\n\n')
n=000009

>>> 
Documentation for print() function.
print() function is implemented in C,source code is here.
thank you snippsat
answered
(Oct-06-2018, 08:54 AM)nzcan Wrote: [ -> ]hi,
i am curious how the 'file' argument of print() could be used.
i am trying:
....
>>> print('hello, world!', file=d) [/python]
but i am receiving the following error:
>>> print('hello, world!', file=d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
io.UnsupportedOperation: not writable 
as it is written in the documentation about 'print()' build-in function ' ... The file argument must be an object with a write(string) method; ... '
then ... why the 'd'-object should not be writable when it has the 'write' method?

It could - if you open a file in a write mode. File objects are subset of stream objects - most of which have write method - but file requires to be opened with write permission to allow writing operation
Output:
In [10]: with open('Groups.ipynb') as f: ...: print(f.writable()) ...: False In [11]: with open('Groups.ipynb', 'w') as f: ...: print(f.writable()) ...: True
In-memory text streams are writable by default
Output:
In [12]: io.StringIO().writable() Out[12]: True