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语句的末尾加上 ...
随机推荐
- tensorflow学习之(二)Seesion的两种打开模式
#Seesion的两种打开模式 import tensorflow as tf matrix1 = tf.constant([[3,3]])#一行两列的一个矩阵 matrix2 = tf.consta ...
- liunx Ubuntu 设置IP、网关、DNS
说明:在网上给的教程上面通常会有这样的一个误导思路,按照配置文件设置后会不生效的问题,甚至没有一点效果,经过排查发现Linux下设置IP这个话题的入口线索应该分为两种:1为Server版,2为Desk ...
- Avoid Inputing Password While Pushing/Pulling Git Project
If we add public key in our git account, we can pull/push project easily without password. However, ...
- Codeforces Round #512 E - Vasya and Good Sequences
有时候觉得自己就是个思路搬运机,只会搬运思路 这个题首先说了求的是好区间的个数, 好区间满足条件: 1.二进制位1的数量和为偶数 2.w[i]表示a[i]的二进制上1的个数 ,sum[i] = ...
- 2.DI依赖注入
一:DI Dependency Injection ,依赖注入 is a :是一个,继承. has a:有一个,成员变量,依赖. class B { private A a; //B类依赖A类 } ...
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...
- 583. Delete Operation for Two Strings
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...
- LOJ#6387 「THUPC2018」绿绿与串串 / String (Manacher || hash+二分)
题目描述 绿绿和 Yazid 是好朋友.他们在一起做串串游戏. 我们定义翻转的操作:把一个串以最后一个字符作对称轴进行翻转复制.形式化地描述就是,如果他翻转的串为 RRR,那么他会将前 ∣R∣−1个字 ...
- python中除法的注意事项
使用python数据处理,代码如下: import numpy as np fs = 5 ts = np.arange(-1,1+1/fs,1/fs) 发现了这样一个错误: Traceback (mo ...
- VSCode配置Git随记
VSCode配置Git随记 2018年05月29日 10:14:24 Dominic- 阅读数:4096 vscode中对git进行了集成,很多操作只需点击就能操作,无需写一些git指令. 不过这 ...