[英]Why is sys.stdout.write outputs unexpected characters?
from sys import stdin, stdout
temp=1
stdout.write(str(temp))
I am expecting output to be 1 but it is 11. Why?我期望 output 为 1,但它是 11。为什么?
>>> 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
.第一个"1"
是您给write
的参数,第二个"1"
是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.请注意,返回值出现在 output 中,因为这是一个交互式 session。如果您从.py
文件运行此代码,您只会看到"Hello"
,如您所料。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.