Python3 print()函数sep,end,file参数用法练习

来自builtins.py:
def print(self, *args, sep=' ', end='\n', file=None): # known special case of 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.
    """
    pass


使用1,练习sep的用法:
print("file\n","abc","bcd","fff\n","poi")
print("-------------")
print("file\n","abc","bcd","fff\n","poi",sep='')
print("-------------")
print("file\n","abc","bcd","fff\n","poi",sep=' ')
print("-------------")
其结果为:
复制代码
file
 abc bcd fff
 poi
-------------
file
abcbcdfff
poi
-------------
file
 abc bcd fff
 poi
-------------
复制代码

使用2,练习end的用法:

1 print("file\n","abc","fff",sep='#########\n',end='')
2 print("--------------------")
3 print("file\n","abc","fff",sep='#########\n',end='\n')
4 print("--------------------")
5 print("file\n","abc","fff",sep='#########\n')
6 print("--------------------")
其结果为:
复制代码
file
#########
abc#########
fff--------------------
file
#########
abc#########
fff
--------------------
file
#########
abc#########
fff
--------------------
复制代码
使用3,练习file的用法:
1 with open('abc.txt','w') as f:
2     print("file\n","abc","fff",sep='#########\n',end='',file=f)
其结果:
将输出写到了文件abc.txt。
abc.txt的内容如下:
file
#########
abc#########
fff

 注意文件末行没有换行符

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值