Python3.7 print() 详解

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.

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.

Changed in version 3.3: Added the flush keyword argument.

print参数是一如既往的可变参数,但是和C中的printf还是有很多区别的。

第一个参数:objects,各个输出变量或者字符串之间用逗号隔开。

第二个参数:sep 表示各个输出字符串之间的间隔字符,默认是空格。

第三个参数:end表示字符串打印结束后的添加的结束字符串,默认是换行。

第四个参数:file表示打印到标准输出,还是文件件中。默认是标准输出。

第五个参数:flush表示是否实时将数据写入file。flash表示只有答应结束的时侯才真正写入到文件,true表示实时写入。默认是flash。

 

另外python中的print也是可以像C一样格式化输出的。如下:

>>> s  
'Hello'  
>>> x = len(s)  
>>> print("The length of %s is %d" % (s,x))  
The length of Hello is 5  

 

看看《Python基础编程》中对格式化输出的总结:

 

(1). %字符:标记转换说明符的开始

(2). 转换标志:-表示左对齐;+表示在转换值之前要加上正负号;“”(空白字符)表示正数之前保留空格;0表示转换值若位数不够则用0填充

(3). 最小字段宽度:转换后的字符串至少应该具有该值指定的宽度。如果是*,则宽度会从值元组中读出。

(4). 点(.)后跟精度值:如果转换的是实数,精度值就表示出现在小数点后的位数。如果转换的是字符串,那么该数字就表示最大字段宽度。如果是*,那么精度将从元组中读出

(5).字符串格式化转换类型

 

转换类型          含义

d,i                 带符号的十进制整数
o                   不带符号的八进制
u                   不带符号的十进制
x                    不带符号的十六进制(小写)
X                   不带符号的十六进制(大写)
e                   科学计数法表示的浮点数(小写)
E                   科学计数法表示的浮点数(大写)
f,F                 十进制浮点数
g                   如果指数大于-4或者小于精度值则和e相同,其他情况和f相同
G                  如果指数大于-4或者小于精度值则和E相同,其他情况和F相同
C                  单字符(接受整数或者单字符字符串)
r                    字符串(使用repr转换任意python对象)
s                   字符串(使用str转换任意python对象)

 

 


 

 

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值