python中stdout是什么意思_python中sys.stdout 与 print的区别

当我们在 Python 中打印对象调用 print obj 时候,事实上是调用了

sys.stdout.write(obj+'\n')

print 将你需要的内容打印到了控制台,然后追加了一个换行符“\n”

print 会调用 sys.stdout 的 write 方法

以下两行在事实上等价:

sys.stdout.write('hello'+'\n')

=

print 'hello'

例如:

#coding=gbk

from sys import stdout

n = input("input number:\n")

print "n = %d" % n

for i in range(2,n + 1):

while (n != i):

if(n%i == 0):

stdout.write(str(i))

stdout.write('*')

n =

n/i

else:

break

print "%d" % n

sys.stdin 与 raw_input

当我们用 raw_input('Input promption: ') 时,事实上是先把提示信息输出,然后捕获输入

以下两组在事实上等价:

hi=raw_input('hello?')

print'hello?',#comma to stay in the same

linehi=sys.stdin.readline()[:-1]#-1

to discard the '\n' in input stream

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值