ssokhey ssokhey - 6 months ago 42
Python Question

Why does write() method returns the length of string in IPython/Jupyter notebook?

I've seen some strange (at least for me) behavior while using write() method in Ipython as well as Jupyter notebook:

In [1]: f = open('test.txt','w')
In [2]: f.write("This is the first line")
Out[2]: 22

It is actually returning the length of the string which I wrote in the file.

I am bit curious about this behavior. Can someone please explain why this happens?


This is not something specific to IPython.

The write() method returns the number of characters written. The reason you are seeing this is because you are using an interpreter.