Search code examples
pythonprintingstdoutstderrsys

Why does `sys.stderr` and `sys.stdout` put a number at the end on the shell, but not in a module - python


Basically my question is the title,

so for example in shell:

>>> import sys
>>> sys.stdout.write('Hello')
Hello5

(same with stderr)

But from a file:

import sys
sys.stdout.write('Hello')

Output:

Hello

(same with stderr)

So why is this happening???


Solution

  • That's the return value. sys.stdout.write('Hello') returns 5, which gets printed automatically in interactive mode, but not in a script.