英文文档:

print(*objectssep=' 'end='\n'file=sys.stdoutflush=False)

Print objects to the text stream file, separated by sep and followed by endsepend 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 Nonesys.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的更多相关文章

  1. python内置函数之print()

    定义:将值打印到一个流对象,或者默认打印到sys.stdout. 语法: print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=Fal ...

  2. Python内置函数(57)——setattr

    英文文档: setattr(object, name, value) This is the counterpart of getattr(). The arguments are an object ...

  3. Python内置函数(50)——print

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

  4. python内置函数print输出到文件,实现日志记录的功能

    # bulid time 2018-6-22 import os import time def log(*args, **kwargs): # *kargs 为了通用 可不传 rule = &quo ...

  5. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  6. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  7. python 内置函数和函数装饰器

    python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...

  8. Python基础篇【第2篇】: Python内置函数(一)

    Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位 ...

  9. python内置函数的归集

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明. Python内置(built-in)函数随着python解释器的运行而创建.在Pytho ...

随机推荐

  1. NancyFX 第六章 视图引擎

    正如其他的Web工具包,Nancy也有视图的概念,用来描述在浏览器上看到的输出 视图的定义 你可能没有之前没有接触过"视图"的概念,或是仅仅是从其他工具包例如ASP.NET MVC ...

  2. cocos creator实现棋牌游戏滑动选牌的功能

    最近在玩cocos creator,打算学着做一款类似双扣游戏的棋牌,名字叫文成三星,比双扣还要多一扣,因为需要三幅牌,在我们老家比较流行这种玩法. 目前实现了绝大部分的逻辑效果如下: 有一点不好的体 ...

  3. 直播-srs起步

    srs简介 https://github.com/ossrs/srs/wiki/v2_CN_Home     原料 CentOS Linux release 7.2.1511 (Core) ffmpe ...

  4. 可视化:svg相关基础

    01.svg的嵌入.html <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  5. sklearn包中有哪些数据集你都知道吗?

    注册了博客园一晃有3个月了,同时接触机器学习也断断续续的算是有1个月了.今天就用机器学习神器sklearn包的相关内容作为我的开篇文章吧. 本文将对sklearn包中的数据集做一个系统介绍,并简单说一 ...

  6. C语言第四次博客作业--嵌套循环

    一.PTA实验作业 题目1:编程打印空心字符菱形 1. 本题PTA提交列表 2. 设计思路(流程图) 3.本题调试过程碰到问题及解决办法 思考过程:将问题拆解为菱形问题和字母变化问题两部分 1> ...

  7. react-router简明学习

    前面的话 路由用来分发请求.后端是提供服务的,所以它的路由是在找controller,前端是显示页面的,所以它的路由是在找component.本文将详细介绍react-router-dom的内容 Ro ...

  8. 笔记:XML-解析文档-DOM

    要处理XML文档,就要先解析(parse)他,解析器时这样一个程序,读入一个文件,确认整个文件具有正确的格式,然后将其分解成各种元素,使得程序员能够访问这些元素,Java库提供了两种XML解析器: 像 ...

  9. angularJs模块ui-router之状态嵌套和视图嵌套

    原文地址:http://bubkoo.com/2014/01/01/angular/ui-router/guide/nested-states%20&%20nested-views/ 状态嵌套 ...

  10. 在nuxt中加入element-ui插件遇到的问题

    gen1.首先进入nuxt的官网跟着步骤实现内容. https://zh.nuxtjs.org/guide/plugins 2.在我们的项目目录中找plugin 根据图片中的表示引入内容: impor ...