4
>>> import sys
>>> sys.stdout.write("Hello")
Hello5
>>> sys.stdout.write("My name is something")
My name is something20
>>>  

As you see on the codes above, the outputs with the length of the string. Wondering why? Why this method display the length? Is there something special that I missed?

GLHF
  • 3,835
  • 10
  • 38
  • 83

1 Answers1

4

In Python3 (though not in Python2), sys.stdout.write returns the number of characters it wrote. Your interpreter is telling you the return value of the function you just called. Because you are printing strings without newlines, it shows up at the end of the string you printed.

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 6
    Whats the point of this and how do I stop it? – Tom Aug 20 '16 at 00:07
  • @Tom because It's not guaranteed to write all the bytes. "This can be less than the length of b in bytes, depending on specifics of the underlying raw stream, and especially if it is in non-blocking mode." – Roger Dahl Mar 13 '20 at 06:26