Python内置函数(50)——print

英文文档:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Print objects to the text stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.

The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.

Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.

 

说明:

  1. 用于对象打印输出。通过命名参数sep来确定多个输出对象的分隔符(默认' '),通过命名参数end确定输出结果的结尾(默认'\n'),通过命名参数file确定往哪里输出(默认sys.stdout),通过命名参数fiush确定输出是否使用缓存(默认False)。

>>> print(1,2,3)
1 2 3
>>> print(1,2,3,sep = '+')
1+2+3
>>> print(1,2,3,sep = '+',end = '=?')
1+2+3=?

  2. sep、end、file、flush都必须以命名参数方式传参,否则将被当做需要输出的对象了。

>>> print(1,2,3,'+','=?')
1 2 3 + =?

  3. sep和end参数必须是字符串;或者为None,为None时意味着将使用其默认值。

>>> print(1,2,3,sep = 97,end = 100)
Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    print(1,2,3,sep = 97,end = 100)
TypeError: sep must be None or a string, not int
>>> print(1,2,3,sep = None,end = None)
1 2 3

  4. 不给print传递任何参数,将只输出end参数的默认值。

>>> print()

>>> print(end = 'by 2016')
by 2016

  5. file参数必须是一个含有write(string) 方法的对象。

>>> class A:
    @staticmethod    
    def write(s):
        print(s)

        
>>> a = A()
>>> print(1,2,3,sep = '+',end = '=?',file = a)
1
+
2
+
3
=?

 

转载于:https://www.cnblogs.com/sesshoumaru/p/6052719.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值