2

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???

U13-Forward
  • 69,221
  • 14
  • 89
  • 114

1 Answers1

6

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

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • This can also be demonstrated by calling `sys.stderr.write('Hello')`, which (on my machine, at least) returns 5, prints a newline, and *then* prints `Hello`. – Green Cloak Guy Sep 10 '18 at 04:12
  • 1
    Wow, now i understand because in python shell it adds an extra print, so in module if i do: `print(sys.stdout.write('Hello'))` it will also add `5` to it, coooool, amazing – U13-Forward Sep 10 '18 at 04:15