gpt4 book ai didi

python - python3 中的 sys.stdout.write 在字符串末尾添加 11

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:22 27 4
gpt4 key购买 nike

有人可以解释为什么 sys.stdout.write()11 附加到我的字符串吗?

$ python3
Python 3.4.3+ (default, Jul 28 2015, 13:17:50)
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 'hello'
>>> y = 'world'
>>> msg = ''
>>> import sys
>>> msg += x
>>> msg += '\n' + y
>>> msg
'hello\nworld'
>>> sys.stdout.write(msg)
hello
world11
>>> print(msg)
hello
world

最佳答案

它不会附加到写入的字符串中。 11这里是sys.stdout.write()的返回值,也就是写入的字符数。

参见 write :

Write the string s to the stream and return the number of characters written.


类似于:

>>> def foo():
... print('something', end='')
... return 42
...
>>> foo()
something42

关于python - python3 中的 sys.stdout.write 在字符串末尾添加 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32576576/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com