Python内置函数(57)——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 and file, 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.
想标准输出对象打印输出
说明:
1. 用于对象打印输出。通过命名参数sep来确定多个输出对象的分隔符(默认' '),通过命名参数end确定输出结果的结尾(默认'\n'),通过命名参数file确定往哪里输出(默认sys.stdout),通过命名参数fiush确定输出是否使用缓存(默认False)。
>>> print(1,2,3)
1 2 3
>>> print(1,2,3,sep = '+')
1+2+3
>>> print(1,2,3,sep = '+',end = '=?')
1+2+3=?
2. sep、end、file、flush都必须以命名参数方式传参,否则将被当做需要输出的对象了。
>>> print(1,2,3,'+','=?')
1 2 3 + =?
3. sep和end参数必须是字符串;或者为None,为None时意味着将使用其默认值。
>>> print(1,2,3,sep = 97,end = 100)
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
print(1,2,3,sep = 97,end = 100)
TypeError: sep must be None or a string, not int
>>> print(1,2,3,sep = None,end = None)
1 2 3
4. 不给print传递任何参数,将只输出end参数的默认值。
>>> print() >>> print(end = 'by 2016')
by 2016
5. file参数必须是一个含有write(string) 方法的对象。
>>> class A:
@staticmethod
def write(s):
print(s) >>> a = A()
>>> print(1,2,3,sep = '+',end = '=?',file = a)
1
+
2
+
3
=?
6.format 参数使用
# 常见形式
print('{0},{1}'.format('zhangk', 32))
print('{},{},{}'.format('zhangk', 'boy', 32))
print('{name},{sex},{age}'.format(age=32, sex='male', name='zhangk'))
# 格式限定符:^ < > 居中 左对齐 右对齐 后面带宽度
l1=['a','bb','ccccc','dddd']
l2=[1,2,333333333333333333,4]
print("{:1<8} {:a>8} {:p^8} {:<8}".format(*l1))
print("{:<8} {:<8} {:<8} {:<8}".format(*l2)) # 精度通常跟类型f 一起使用
print('{:.2f}'.format(21.42423423)) # 其他类型
# 主要就是进制了,b、d、o、x分别是二进制、十进制、八进制、十六进制
print('{:b}'.format(15))
print('{:d}'.format(15))
print('{:o}'.format(15))
print('{:x}'.format(15)) # 用逗号还能用来做金额的千位分隔符
print('{:,}'.format(123456789)
Python内置函数(57)——print的更多相关文章
- python内置函数之print()
定义:将值打印到一个流对象,或者默认打印到sys.stdout. 语法: print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=Fal ...
- Python内置函数(57)——setattr
英文文档: setattr(object, name, value) This is the counterpart of getattr(). The arguments are an object ...
- Python内置函数(50)——print
英文文档: print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) Print objects to the text str ...
- python内置函数print输出到文件,实现日志记录的功能
# bulid time 2018-6-22 import os import time def log(*args, **kwargs): # *kargs 为了通用 可不传 rule = &quo ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
- Python基础篇【第2篇】: Python内置函数(一)
Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位 ...
- python内置函数的归集
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明. Python内置(built-in)函数随着python解释器的运行而创建.在Pytho ...
随机推荐
- HTTP架构介绍(2) 缓存
web缓存是自动复制所请求数据并将其保存在本地存储中的设备. 通过这样做, 可以实现: 减少网络流量 消除网络瓶颈 防止服务器超载 减少长距离的响应延迟 因此, 您可以清楚地说, web 缓存可提高用 ...
- Ansible学习总结(1)
---恢复内容开始--- 1. Ansible概述 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric) ...
- windows7+python2.7+setuptools 、pip+Django+PIL 网页制作环境配置
1.安装Python2.7 官网下载安装包python-2.7.amd64,对应64位.安装时没什么特别操作,安装后将python的安装路径添加到系统变量path中,步骤为程序->计算机-> ...
- .NET Core开源API网关 – Ocelot中文文档
Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,包括了:路由.请求聚合.服务发现.认证.鉴权.限流熔断.并内置了负载均衡器与Service Fabric.Butterfly ...
- Java equals() 和hashCode()方法详解
Java的Object类中定义了equals方法,Object类中的equals方法源代码如下,从源代码中可以看出Object类中的equals方法是用来返回判断两个对象是否指向同一个对象(引用地址) ...
- V5.7_UTF8_SP1、SP2---任意前台用户登录(cookie伪造)
漏洞触发点在include/memberlogin.class.php中的MemberLogin类中的登录校验函数 可以看到M_ID参数是由GetNum(GetCookie("DedeUse ...
- push_back和emplace_back的区别
emplace_back能就地通过参数构造对象,不需要拷贝或者移动内存,相比push_back能更好地避免内存的拷贝与移动,使容器插入元素的性能得到进一步提升.在大多数情况下应该优先使用emplace ...
- 面试题中遇到的算法与js技巧
近一周在忙着面试,本月第一次更博,甚是想念. 基本上大公司都会要求一些算法或者数据结构类的东西,这方面自己还不是很精通,只能一步一个脚印来积累了. 1.查询字符串获取对象数据,可自行根据需求选择格式, ...
- JS图片更换还原操作,通过图片识别标识
//图片更换还原操作,图片识别标识 如图片img.png 可换成 img2.png function img_biaoshi(caozuo,img_id, biaoshi) { var img_src ...
- string和c_str()使用时的坑
先看一段代码和它的运行结果: 看到结果了么这个运行的结果和我们理解的是不会有差距.对于经验丰富的开发者可能会微微一笑,但是对于一个刚刚学习的人就开始疑惑了.这里主要说两个问题: 1.声明了一个stri ...