Built-in Functions - 内置函数 - print()

https://docs.python.org/zh-cn/3/library/functions.html
https://docs.python.org/3/library/functions.html

1. 2to3 - Automated Python 2 to 3 code translation (2to3 - 自动将 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 chengProcess 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_functiondef 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 timedef 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 时,它会立即把内容刷新输出。

Built-in Functions - 内置函数 - print()相关推荐

  1. 转义字符'\r'在Python内置函数print()中的妙用

    在Python 3.x中,内置函数print()用来实现格式化输出,各参数含义请参考本文末尾的相关阅读.本文重点介绍print()函数的end参数以及转义字符'\r'的妙用. 本文末尾的相关阅读中已经 ...

  2. Python中的标准库函数(内置函数)print()输出(打印出)字符串的常见用法

    这篇博文用于记录下Python中的标准库函数print()的常见用法,随着时间的推移,可能会有更新. print 在 Python3.x 是一个函数,但在 Python2.x 版本不是一个函数,只是一 ...

  3. python内置输入函数_python内置函数 print()

    英文文档: print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) Print objects to the text str ...

  4. 【python】python内置函数——print()打印输出信息

    print()用于打印输出. print在python2中是一个关键字,使用时不必加括号:但在python3中是一个函数,使用时需要加括号.python3.3版本增加了flush参数. print() ...

  5. day4 匿名函数、装饰器、生成器、迭代器、内置函数、 json 与 pickle 模块

    文章目录 1.列表生成式 2.匿名函数 3.装饰器 4.生成器 5.迭代器 6.内置函数 7.json & pickle 序列化与反序列化 1.列表生成式 可通过特定语句便捷生成列表 list ...

  6. python内置函数open_Python的内置函数open()的注意事项

    用法 : open("file_address","open_mode") 例子 : f = open("D:\PycharmProjects\log ...

  7. python求最小值不能使用min和sotred_python基础——内置函数

    python基础--内置函数  一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highl ...

  8. python __call__一般用在哪些地方_Python __call__内置函数的作用和用法

    开学了进入了实验室,需要协助大师兄做事,主要是OpenStack中的代码解析,但是涉及很多python高级用法,一时间有点麻烦,在做项目的同时慢慢更新博客.这次先写一下__call__的用法,因为经常 ...

  9. Python内置函数(62)——exec

    英文文档: exec(object[, globals[, locals]])This function supports dynamic execution of Python code. obje ...

最新文章

  1. 纯css3鼠标经过出现文字或图片鼠标移走消失
  2. 衔接上一学期:排球积分规则
  3. 类库 通用变量 is和as 委托
  4. jar包天天见,可是你知道它的运行机制吗
  5. Confluence 6 选择一个外部数据库
  6. 软件测试--面试时如何回答接口测试怎么进行
  7. C语言变量声明加冒号的用法
  8. hook监控限制_**CodeIgniter通过hook的方式实现简单的权限控制
  9. 支持ipv6路由器有什么优点?路由器应用了哪些技术?
  10. 分布式事务的四种解决方案
  11. Unbalanced calls to begin/end appearance transitions for
  12. 【onnx】——since it‘s not constant, please try to make things (e.g., kernel size) static if possible
  13. 【汇智学堂】docker网络管理之二
  14. 信息安全系统所需要遵循的基本原则有哪些?
  15. 专知 2019/4/24(图像填充方法大全)
  16. 计算机数列类型,斐波那契(Fibonacci)数列的几种计算机解法
  17. 已知二叉树先序序列和中序序列,求后序序列
  18. vs2017打开项目后项目是空的
  19. 投身自媒体的普通人:他们是如何从从月入30到月入3万的
  20. 天津市“多规合一”信息资源目录体系建设

热门文章

  1. 阿里云服务器美国西部 1、美国西部 2、美国东部 1、 美国东部 2是哪个城市
  2. 生物学不必以我们已经知道的形式存在|无学科专栏
  3. 前瞻2022:国际粮价创十年新高 中国粮价会怎么走、
  4. 数据库设计冗余_域和数据库设计中的冗余
  5. Android获取本地相册图片
  6. el-table后端返回日期2023-04-07T09:10:47.000+00:00格式转换
  7. 清华计算机系非全,清华2020级硕博研究生生源大揭秘,非全日制占23%,博士最小18岁...
  8. 将java项目导出jar包,然后转成在windows上的可执行文件(没有java运行环境的电脑也可以)
  9. MySQL:MySQLWorkbench配置字体与颜色主题
  10. php soap传输xml,通过PHP SoapClient请求发送原始XML