英文文档:
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 =?