Difference between sys.stdout.write and print when printing variables

python, python-3.x

Solution

The return value for `sys.stdout.write()` returns the no. of bytes written. In this case `1` which also gets printed on the interactive interpret prompt for any expressions entered.

Example:

>>> import sys
>>> sys.stdout.write("foo")
foo3

If you wanted to hide this you could do this:

>>> nbytes = sys.stdout.write("foo\n")
foo

Problem

I would like to learn the difference between `sys.stdout.write` and `print` methods(or functions? Should I call them function or method?) For example, the code below will print 11 ``` a = str(1) sys.stdout.write (a) ``` But this will print 1 ``` a = str(1) print (a) ``` Why is there such difference? Is there any way to make sys.stdout.write() print 1, not 11? Thanks!

Original source

Related problems