4
from sys import stdin, stdout
temp=1
stdout.write(str(temp))

I am expecting output to be 1 but it is 11 . Why?

2
  • The problem is likely to be the lack of newline and that at some earlier or later point in your code another 1 is being written. If you want to write a newline, then you have to do so explicitly, e.g. sys.stdout.write(str(temp) + '\n'), unlike with print(). Commented Jun 19, 2020 at 17:55
  • if I go with sys.stdout.write(str(temp)+'\n') then output is 1 2.I only want 1 as a output Commented Jun 19, 2020 at 17:59

1 Answer 1

7
>>> import sys
>>> help(sys.stdout.write)

Help on built-in function write:

write(text, /) method of _io.TextIOWrapper instance
    Write string to stream.
    Returns the number of characters written (which is always equal to
    the length of the string)

The first "1" is the argument that you give to write and the second "1" is length of your string returned by write.

>>> sys.stdout.write("Hello")
Hello5

Note that the return value appears in the output because this is an interactive session. If you run this code from a .py file, you would only see "Hello", as you expected.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.