Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python print函数参数详解以及效果展示

官方文档

print(…)
print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.


参数解析
value:需要输出的值,可以是多个,用”,”分隔。
sep:多个输出值之间的间隔,默认为一个空格。
end:输出语句结束以后附加的字符串,默认是换行(’\n’)。
file:输出的目标对象,可以是文件也可以是数据流,默认是“sys.stdout”。
flush:flush值为True或者False,默认为Flase,表示是否立刻将输出语句输出到目标对象。


演示

默认:print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)

>>> print("hello world")
hello world

当value有多个:

>>> print("hello","world")
hello world

当sep为”,”

>>> print("hello","world",sep=",")
hello,world

当end为“”

>>>print("hello","world",end="") 
>>>print("hello","world")
hello worldhello world

当file指向test.txt

test = open("test.txt", "w")
print("hello","world",sep="\n", file=test)

此时当前目录下会新建一个test.txt文件里面内容为

hello
world

flush=False
该参数只有两个选项True or False。
当flush=False时,输出值会存在缓存,然后在文件被关闭时写入。
当flush=True时,输出值强制写入文件。

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值