Python Built-in Functions {内置函数} print{}
- 1. `2to3` - Automated Python 2 to 3 code translation (自动将 Python 2 代码转为 Python 3 代码)
- 2. `print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)`
- 3. Python 2.x `print` 语句 (`print` statement) 不换行
- 4. Python 3.x `print()` 函数 (`print()` function) 不换行
- 5. Python 2.x `print` 语句 (`print` statement) 不换行
- 6. `flush` 参数
- References
Built-in Functions (内置函数)
https://docs.python.org/3/library/functions.html
https://docs.python.org/zh-cn/3/library/functions.html
1. 2to3
- Automated Python 2 to 3 code translation (自动将 Python 2 代码转为 Python 3 代码)
https://docs.python.org/3/library/2to3.html
2to3
is a Python program that reads Python 2.x source code and applies a series of fixers to transform it into valid Python 3.x code. The standard library contains a rich set of fixers that will handle almost all code. 2to3
supporting library lib2to3
is, however, a flexible and generic library, so it is possible to write your own fixers for 2to3
. lib2to3
could also be adapted to custom applications in which Python code needs to be edited automatically.
2to3
是一个 Python 程序,它可以用来读取 Python 2.x 版本的代码,并使用一系列的修复器来将其转换为合法的 Python 3.x 代码。标准库中已经包含了丰富的修复器,这足以处理绝大多数代码。不过 2to3
的支持库 lib2to3
是一个很灵活通用的库,所以你也可以为 2to3
编写你自己的修复器。lib2to3
也可以用在那些需要自动处理 Python 代码的应用中。
Converts the print
statement to the print()
function.
将 print
语句转换为 print()
函数。
Python 2.x code:print
语句 (print
statement)
Python 3.x code:print()
函数 (print()
function)
2. 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
, file
and flush
, if present, must be given as keyword arguments.
将 objects 打印到 file
指定的文本流,以 sep
分隔并在末尾加上 end
。sep
、end
、file
和 flush
如果存在,它们必须以关键字参数的形式给出。
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.
所有非关键字参数都会被转换为字符串,就像是执行了 str()
一样,并会被写入到流,以 sep
且在末尾加上 end
。sep
和 end
都必须为字符串,它们也可以为 None
,这意味着使用默认值。如果没有给出 objects,则 print()
将只写入 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.
file
参数必须是一个具有 write(string)
方法的对象。如果参数不存在或为 None
,则将使用 sys.stdout
。由于要打印的参数会被转换为文本字符串,因此 print()
不能用于二进制模式的文件对象。对于这些对象,应改用 file.write(...)
。
Whether output is buffered is usually determined by file
, but if the flush
keyword argument is true, the stream is forcibly flushed.
输出是否被缓存通常决定于 file
,但如果 flush
关键字参数为真值,流会被强制刷新。
第二个参数 sep
,表示 objects 参数连接时使用的字符,默认是空格。
第四个参数 file
,表示输出到哪里,默认是 sys.stdout
。
第五个参数 flush
,表示是否立即输出到 file
所指定的对象中。当为 True
时,立即输出。当为 False
时,则取决于 file
对象 (一般不立即输出)。
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Users\foreverstrong>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("yongqiang", "cheng", sep='*')
yongqiang*cheng
>>> exit()
C:\Users\foreverstrong>
3. Python 2.x print
语句 (print
statement) 不换行
在 print
语句句尾加上逗号实现不换行,默认换行。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def main():
print "yong"
print "qiang"
print "cheng"
print "yong",
print "qiang",
print "cheng"
if __name__ == "__main__":
main()
/usr/bin/python2.7 /home/strong/darknet_work/darknet_20190629/darknet/yongqiang.py
yong
qiang
cheng
yong qiang cheng
Process finished with exit code 0
4. Python 3.x print()
函数 (print()
function) 不换行
end
参数来指定结束时输出的字符,默认换行。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def main():
print("yong")
print("qiang")
print("cheng")
print("yong", end=" ")
print("qiang", end=" ")
print("cheng", end=" ")
print()
print("yong", end="")
print("qiang", end="")
print("cheng", end="")
if __name__ == "__main__":
main()
/usr/bin/python3.5 /home/strong/darknet_work/darknet_20190629/darknet/yongqiang.py
yong
qiang
cheng
yong qiang cheng
yongqiangcheng
Process finished with exit code 0
5. Python 2.x print
语句 (print
statement) 不换行
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
def main():
print("yong")
print("qiang")
print("cheng")
print("yong", end=" ")
print("qiang", end=" ")
print("cheng", end=" ")
print()
print("yong", end="")
print("qiang", end="")
print("cheng", end="")
if __name__ == "__main__":
main()
/usr/bin/python2.7 /home/strong/darknet_work/darknet_20190629/darknet/yongqiang.py
yong
qiang
cheng
yong qiang cheng
yongqiangcheng
Process finished with exit code 0
6. flush
参数
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
def main():
print("Loading", end="")
for i in range(6):
print(".", end='', flush=True)
time.sleep(0.2)
if __name__ == "__main__":
main()
/usr/bin/python3.5 /home/strong/darknet_work/darknet_20190629/darknet/yongqiang.py
Loading......
Process finished with exit code 0
flush
参数主要是刷新,默认 flush=False
,不刷新。当 flush=True
时,它会立即把内容刷新输出。
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/