Python 3 进阶 —— print 打印和输出
在 Python 中,
在 2.x 版本中,
参数选项
可以用 help(print)
来查看 print
函数的参数解释。
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.
- value: 打印的值,可多个
- file: 输出流,默认是
sys.stdout
- sep: 多个值之间的分隔符
- end: 结束符,默认是换行符
\n
- flush: 是否强制刷新到输出流,默认否
能打印任意数据
- 打印数字、字符串、布尔值
print(1024, 10.24, 'hello', False)
# 1024 10.24 hello False
- 打印列表
print([1, 2, 3])
# [1, 2, 3]
- 打印元组
print((1, 2, 3))
# (1, 2, 3)
- 打印字典
print({'name': 'hello', 'age': 18})
# {'name': 'hello', 'age': 18}
- 打印集合
print({1, 2, 3})
# {1, 2, 3}
- 打印对象
class Demo:
pass
demo = Demo()
print(demo)
# <__main__.Demo object at 0x1005bae80>
分隔符
默认分隔符是空格,sep
参数可以修改。
print(1, 2, 3, sep='-')
# 1-2-3
结束符
默认结束符是行号,end
参数可以修改。
print('第一行', end='-')
print('第二行')
# 第一行-第二行
输出重定向
默认情况下,print
函数会将内容打印输出到标准输出流(即 sys.stdout),可以通过 file 参数自定义输出流。
with open('data.log', 'w') as fileObj:
print('hello world!', file=fileObj)
此时,不会有任何标准输出,但对应的文件中已经有了内容。
我们也可以输出到错误输出流,例如:
import sys
print('hello world!', file=sys.stderr)
参考资料
个人博客同步地址:
https://shockerli.net/post/python3-print/
Python 3 进阶 —— print 打印和输出的更多相关文章
- Python 入门 之 print带颜色输出
Python 入门 之 print带颜色输出 1.print带颜色输出书写格式: 开头部分: \033[显示方式; 前景色 ; 背景色 m 结尾部分: \033[0m 详解: 开头部分的三个参数: 显 ...
- Python学习4——print打印
print(): 在控制台输出变量的值: print打印完后换行: print(123) # 完整模式:print(123,end="\n") 希望打印完不换行: print(1 ...
- Python中使用print打印进度条
import time for i in range(0,101,2): time.sleep(0.1) char_num = i//2 #打印多少个'*' per_str = '\r%s%% : % ...
- Python input和print函数
一.input函数 可以看出,input()函数默认输入的是字符串类型,需要eval()函数将其进行转换. 区别直接赋值的情况,Python可以自动识别数据类型 二.print函数 1.直接输出 无论 ...
- Python 让两个print()函数的输出打印在一行内
1.两个连续的print()函数为什么在输出时内容会分行显示? 解:print()中有两个默认参数 sep 和 end,其中sep是代替分隔符,end是代替末尾的换行符,默认使用‘,’代替空格,且默认 ...
- 【原创】python中文编码问题深入分析(二):print打印中文异常及显示乱码问题分析与解决
在学习python以及在使用python进行项目开发的过程中,经常会使用print语句打印一些调试信息,这些调试信息中往往会包含中文,如果你使用python版本是python2.7,或许你也会遇到和我 ...
- 【Python】Python 打印和输出更多用法。
Python 打印和输出 简述 在编程实践中,print 的使用频率非常高,特别是程序运行到某个时刻,要检测产生的结果时,必须用 print 来打印输出. 关于 print 函数,前面很多地方已经提及 ...
- Python中的输入(input)和输出打印
目录 最简单的打印 打印数字 打印字符 字符串的格式化输出 python中让输出不换行 以下的都是在Python3.X环境下的 使用 input 函数接收用户的输入,返回的是 str 字符串 最简单的 ...
- Python使用print打印时,展示内容不换行
原理 Python的print()函数中参数end='' 默认为\n,所以会自动换行; 默认的print()函数: print(end='\n') 方案 Python 2: 在print语句的末尾加上 ...
随机推荐
- eclipse启动时出现无法创建java虚拟机
最 近一直在用eclipse开发android程序,今天不知怎么的启动eclipse时就会出现Failed to create java virtual machine,无法打开eclipse程序,折 ...
- diango中的url路由系统
一.url配置 url本质是url与要为该url调用的视图函数之间的映射表 urlpatterns = [正则,视图函数[,别名]] 二.正则表达式 1.匹配原则 django是循环urlpatter ...
- Struts2学习第四天——全局结果,动态结果及异常映射
1.异常映射的配置 当Action方法出错时Struts会返回异常错误信息页面,这种页面方式是不友好的,可以使用try-catch捕捉异常,然后在catch块中返回对应的错误页面.这种为单个<a ...
- rpm is for architecture aarch64 ; the package cannot be built on this system
问题:rpm is for architecture aarch64 ; the package cannot be built on this system $ sudo alien --to-de ...
- WebRTC 学习之 WebRTC 简介
本文使用的WebRTC相关API都是基于Intel® Collaboration Suite for WebRTC的. 相关文档链接:https://software.intel.com/sites/ ...
- video设置autoplay 不起作用
video 标签上添加 muted https://blog.csdn.net/taiyangmiaomiao/article/details/80266625
- Shell-3--变量
用户自定义变量 环境变量 位置参数变量 预定义变量
- IDEA Exception in thread "main" java.lang.ClassNotFoundException: com.streamax.servicecore.business.FileManageServApplication
[参考文章]:intelij idea: Exception in thread "main" java.lang.ClassNotFoundException 1. 报错信息 2 ...
- Mobility Model and Routing Model about the ONE
ONE主要的功能是节点的移动,节点间的相遇情况,路由情况以及消息的处理机制.下面简要介绍下目前ONE自带的六种移动模型和六种路由模型. Mobility Model: 从大的方面,可以分为三种,1)随 ...
- SVN 分支主干的相互合并
1.主干合并到分支 1在本地trunk中先update一下,有冲突的解决冲突,保证trunk和repository已经完全同步, 2.在/branches /MyProject上右键,依次选择”Tor ...